正在学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();就提示错误
求高手指点 我知道我很菜 请不要再喷我了
解决方案 »
- android中dialog弹出软键盘问题
- 请问Android的加速度传感器和重力传感器有什么区别呢?
- 关于关闭ProgressDialog
- 讨论Android中游戏技能冷却效果的实现思想!
- 把android中的linpng移植到windows mobile上
- android怎么实现接收充电广播
- 【快马加鞭】 急求!!android ndk 使用 fstream 在编译时总是no file find。
- Android通过点击启动其他程序要怎么实现?
- 新手求助!设计一个android听力考试app,每道题都对应一段录音,录音应该存成文件还是存在SQLite数据库?
- android短信拦截源码
- repo获取源代码出错,求助, 谢谢!
- 可以在pc上安装andriod吗
你把login类的方法public login(String username,String password,Context c) throws OptionalDataException, ClassNotFoundException, IOException{
挪到loginActivity
然后ua = (userApplication) this.getApplicationContext()或者要非要在login这个类中获得Application对象,那就传个Context对象过去