正在学application
所有的活动都部署在package development.samples.helloAndroid;
以下是期中一个loginActivity 代码:package development.samples.helloAndroid;import java.io.IOException;
import java.io.OptionalDataException;import Android.Album.account.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import development.samples.helloAndroid.getAlbumsActivity;
public class loginActivity extends Activity {
Button but;
EditText et1,et2;
String userName;
String password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
but = (Button)this.findViewById(R.id.btn_OK);
et1 = (EditText)this.findViewById(R.id.username);
et2 = (EditText)this.findViewById(R.id.password);
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userName = et1.getText().toString().trim();
password = et2. getText().toString().trim();
//Toast.makeText(loginActivity.this, password, Toast.LENGTH_LONG).show();
try {
login l = new login(userName,password,loginActivity.this);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent openAlbum = new Intent();
openAlbum.setClass(loginActivity.this, getAlbumsActivity.class);
startActivity(openAlbum);
}
}) ; }
}以下是期中login的代码
public class login {
@SuppressWarnings("unused")
public login(String username,String password,Context c) throws OptionalDataException, ClassNotFoundException, IOException{
String url1 = "http://113.55.37.56" +
":8080/AlbumOnline/login";
String url2 = "?userName="+username+"&password="+password;
String url = url1 + url2;
//Toast.makeText(c, username, Toast.LENGTH_LONG).show();
//Toast.makeText(c, password, Toast.LENGTH_LONG).show();
sendRequest sr = new sendRequest(url,c);
InputStream is = sr.getInputStream();
//Toast.makeText(c, , Toast.LENGTH_LONG).show();
UserList luser = new UserList();
ObjectInputStream ois = new ObjectInputStream(is);
luser = (UserList)ois.readObject();
userApplication ua = new userApplication();
ua = (userApplication) ua.getApplicationContext(); ua.setUser(luser);
UserList luser2 = new UserList();
luser2 = ua.getUser();
Toast.makeText(c,"luser2name:"+luser2.getUserName(), Toast.LENGTH_LONG).show();
if(luser!=null){
//String username1 = user.getUserName();
//Toast.makeText(c, "user 得到了啊", Toast.LENGTH_LONG).show(); Toast.makeText(c,luser.getUserName(), Toast.LENGTH_LONG).show();
/* List<FriendList> fs = luser.getFriends();
Iterator<FriendList> iter = fs.iterator();
*/
List<AlbumList>as=luser2.getAlbums();
Iterator<AlbumList> iter = as.iterator();
while (iter.hasNext()){
/*FriendList f = new FriendList();
f = (FriendList)iter.next();
*/
AlbumList a = new AlbumList();
a = iter.next();
Toast.makeText(c,a.getAlbName(), Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(c, "error: user is null", Toast.LENGTH_LONG).show();
}
}
}
以下是application代码:package development.samples.helloAndroid;import java.io.IOException;
import java.io.OptionalDataException;import Android.Album.account.login;
import Android.Album.albumImp.albumSpinnerAdapter;
import Android.Album.albumImp.getAlbum;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.app.Application;import com.demo.hibernate.beans.UserList;
public class userApplication extends Application {
private UserList user;
public void setUser(UserList user){
this.user = user;
}
public UserList getUser(){
return this.user;
}
}以下是AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="development.samples.helloAndroid" android:versionCode="1"
android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".userApplication">
<activity android:name=".HelloAndroid" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Allcolor" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChangeLayout" android:label="changeLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".Activity2" android:label="Activity2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity3" android:label="Activity3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FetchPicTest" android:label="FetchPic">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GetObject" android:label="GetObject">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloGallery" android:label="GetG">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestNet" android:label="testNet">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".loginActivity" android:label="login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".getAlbumsActivity" android:label="Albums">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>每次运行到 login中ua = (userApplication) ua.getApplicationContext();就提示错误
求高手指点 我知道我很菜 请不要再喷我了
所有的活动都部署在package development.samples.helloAndroid;
以下是期中一个loginActivity 代码:package development.samples.helloAndroid;import java.io.IOException;
import java.io.OptionalDataException;import Android.Album.account.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import development.samples.helloAndroid.getAlbumsActivity;
public class loginActivity extends Activity {
Button but;
EditText et1,et2;
String userName;
String password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
but = (Button)this.findViewById(R.id.btn_OK);
et1 = (EditText)this.findViewById(R.id.username);
et2 = (EditText)this.findViewById(R.id.password);
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userName = et1.getText().toString().trim();
password = et2. getText().toString().trim();
//Toast.makeText(loginActivity.this, password, Toast.LENGTH_LONG).show();
try {
login l = new login(userName,password,loginActivity.this);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent openAlbum = new Intent();
openAlbum.setClass(loginActivity.this, getAlbumsActivity.class);
startActivity(openAlbum);
}
}) ; }
}以下是期中login的代码
public class login {
@SuppressWarnings("unused")
public login(String username,String password,Context c) throws OptionalDataException, ClassNotFoundException, IOException{
String url1 = "http://113.55.37.56" +
":8080/AlbumOnline/login";
String url2 = "?userName="+username+"&password="+password;
String url = url1 + url2;
//Toast.makeText(c, username, Toast.LENGTH_LONG).show();
//Toast.makeText(c, password, Toast.LENGTH_LONG).show();
sendRequest sr = new sendRequest(url,c);
InputStream is = sr.getInputStream();
//Toast.makeText(c, , Toast.LENGTH_LONG).show();
UserList luser = new UserList();
ObjectInputStream ois = new ObjectInputStream(is);
luser = (UserList)ois.readObject();
userApplication ua = new userApplication();
ua = (userApplication) ua.getApplicationContext(); ua.setUser(luser);
UserList luser2 = new UserList();
luser2 = ua.getUser();
Toast.makeText(c,"luser2name:"+luser2.getUserName(), Toast.LENGTH_LONG).show();
if(luser!=null){
//String username1 = user.getUserName();
//Toast.makeText(c, "user 得到了啊", Toast.LENGTH_LONG).show(); Toast.makeText(c,luser.getUserName(), Toast.LENGTH_LONG).show();
/* List<FriendList> fs = luser.getFriends();
Iterator<FriendList> iter = fs.iterator();
*/
List<AlbumList>as=luser2.getAlbums();
Iterator<AlbumList> iter = as.iterator();
while (iter.hasNext()){
/*FriendList f = new FriendList();
f = (FriendList)iter.next();
*/
AlbumList a = new AlbumList();
a = iter.next();
Toast.makeText(c,a.getAlbName(), Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(c, "error: user is null", Toast.LENGTH_LONG).show();
}
}
}
以下是application代码:package development.samples.helloAndroid;import java.io.IOException;
import java.io.OptionalDataException;import Android.Album.account.login;
import Android.Album.albumImp.albumSpinnerAdapter;
import Android.Album.albumImp.getAlbum;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.app.Application;import com.demo.hibernate.beans.UserList;
public class userApplication extends Application {
private UserList user;
public void setUser(UserList user){
this.user = user;
}
public UserList getUser(){
return this.user;
}
}以下是AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="development.samples.helloAndroid" android:versionCode="1"
android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".userApplication">
<activity android:name=".HelloAndroid" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Allcolor" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChangeLayout" android:label="changeLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".Activity2" android:label="Activity2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity3" android:label="Activity3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FetchPicTest" android:label="FetchPic">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GetObject" android:label="GetObject">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloGallery" android:label="GetG">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestNet" android:label="testNet">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".loginActivity" android:label="login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".getAlbumsActivity" android:label="Albums">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>每次运行到 login中ua = (userApplication) ua.getApplicationContext();就提示错误
求高手指点 我知道我很菜 请不要再喷我了
解决方案 »
- 怎么用fragment实现tabhost的功能
- android settings ballery中的电量蓝线 断断续续的是什么原因
- android 如何实现多级树形结构图(至少5级)
- LayoutInflater.from(context) 空指针错误
- eclipse开发android系统输出文字问题
- Activity后,Activity中的showDialog会报错,求帮忙...
- 动画的android_startOffset无效.rtf
- 如何将手机中的csv文件的内容导入到SQLite数据库中
- ART JNI調用中的art_jni_dlsym_lookup_stub入口處在哪裡
- 中国天气网提供的数据只有6天的,为什么?
- repo获取源代码出错,求助, 谢谢!
- 可以在pc上安装andriod吗
你把login类的方法public login(String username,String password,Context c) throws OptionalDataException, ClassNotFoundException, IOException{
挪到loginActivity
然后ua = (userApplication) this.getApplicationContext()或者要非要在login这个类中获得Application对象,那就传个Context对象过去