1
我有一個ListView列出了一個項目列表(項目名稱)。此ListView由AsyncTask初始化。這個AsyncTask在活動開始時被調用。當我嘗試回到父活動時的意圖問題
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projects);
portal = LoginActivity.getPortal();
activity = this;
viewListProjects = (ListView)findViewById(R.projects.list);
create = (Button)findViewById(R.projects.create);
listItem = new ArrayList<HashMap<String, String>>();
this.currentItemClicked = null;
adapter = new SimpleAdapter(
this.getBaseContext(),
listItem,
R.layout.list_projects_item,
new String[] {"title", "date"},
new int[] {R.list_projects_item.title, R.list_projects_item.date}
);
viewListProjects.setAdapter(adapter);
viewListProjects.setOnItemClickListener(itemListener);
create.setOnClickListener(createListener);
String message = "Loading...";
Toast.makeText (this, message, Toast.LENGTH_SHORT).show();
CAsyncTask1 task = new CAsyncTask1();
task.execute();
}
private class CAsyncTask1 extends AsyncTask<Void, Void, Void> {
protected Void doInBackground (Void... unused) {
new Thread() {
public void run() {
listProjects = portal.getAllProjects();
activity.runOnUiThread (new Runnable() {
public void run() {
if(listProjects == null){
Toast.makeText(getBaseContext(), "Connection Failed!", Toast.LENGTH_SHORT).show();
activity.finish();
}
else{
HashMap<String, String> map = new HashMap<String, String>();
for(Info info : listProjects){
map.put("title", info.getName());
map.put("date", info.getCreation().toString());
listItem.add(map);
adapter.notifyDataSetChanged();
}
}
} // run();
});
} // run();
}.start();
return null;
} // doInBackground();
protected void onPostExecute (final Void unused) {
String message = "Project list updated!";
Toast.makeText (activity, message, Toast.LENGTH_SHORT).show();
} // onPostExecute();
} // CAsyncTask();
我創建按鈕點擊創建一個新的項目
private OnClickListener createListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ProjectsActivity.this, NewProjectActivity.class);
startActivity(intent);
}
};
這裏,使創建一個新項目的NewProjectActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_project);
portal = LoginActivity.getPortal();
name = (EditText) findViewById(R.new_project.name);
cancel = (Button) findViewById(R.new_project.cancel);
create = (Button) findViewById(R.new_project.create);
cancel.setOnClickListener(cancelListener);
create.setOnClickListener(createListener);
}
private OnClickListener createListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(name.getText().toString() != ""){
Project project = new Project();
Info info = new Info(name.getText().toString());
project.setInfos(info);
portal.createProject(project);
Intent intent = new Intent(NewProjectActivity.this, ProjectsActivity.class);
startActivity(intent);
}
else{
Toast.makeText(getBaseContext(), "Please give a name to your project.", Toast.LENGTH_SHORT).show();
}
}
};
當我打電話意向意圖=新的意圖(NewProjectActivity.this,ProjectsActivity.class),我得到一個例外:
01-26 03:23:23.493: E/AndroidRuntime(22440): FATAL EXCEPTION: main
01-26 03:23:23.493: E/AndroidRuntime(22440): java.lang.NullPointerException
01-26 03:23:23.493: E/AndroidRuntime(22440): at org.esir.android.ProjectsActivity$CAsyncTask1$1$1.run(ProjectsActivity.java:189)
01-26 03:23:23.493: E/AndroidRuntime(22440): at android.os.Handler.handleCallback(Handler.java:587)
01-26 03:23:23.493: E/AndroidRuntime(22440): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 03:23:23.493: E/AndroidRuntime(22440): at android.os.Looper.loop(Looper.java:130)
01-26 03:23:23.493: E/AndroidRuntime(22440): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-26 03:23:23.493: E/AndroidRuntime(22440): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 03:23:23.493: E/AndroidRuntime(22440): at java.lang.reflect.Method.invoke(Method.java:507)
01-26 03:23:23.493: E/AndroidRuntime(22440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-26 03:23:23.493: E/AndroidRuntime(22440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-26 03:23:23.493: E/AndroidRuntime(22440): at dalvik.system.NativeStart.main(Native Method)
我試圖找到一個解決方案2周...所以,請幫助我。
您是否在manifest.xml文件中聲明瞭NewProjectActivity? – ghostbust555
另外在AsyncTask的另一個線程內部創建一個新線程肯定不被推薦 – ghostbust555
Logcat顯示'ProjectsActivity.java:189',所以錯誤發生在189行。哪一行'ProjectsActivity'是189行?基於你的Logcat,異常與意圖無關,而且它實際上在'ProjectsActivity'中的'AsyncTask'中。 – Jakar