我有一个列出项目列表(项目名称)的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();
}
}
};
当我调用Intent intent = new Intent(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周......所以,请帮助我。
答案 0 :(得分:0)
我想 activity.runOnUiThread(new Runnable(){
此行活动必须为空。
您可以使用处理程序或选择视图并发布runnable而不是活动实例。