尝试在我的应用程序中实现启动画面后,我遇到了强制关闭问题。
问题发生在第 76 行, lv.setAdapter(adapter);
但我不确定为什么。
非常感谢任何输入。
09-19 15:20:53.687: E/AndroidRuntime(25177): FATAL EXCEPTION: main
09-19 15:20:53.687: E/AndroidRuntime(25177): java.lang.NullPointerException
09-19 15:20:53.687: E/AndroidRuntime(25177): at com.example.project1.MainActivity$MyTask.onPostExecute(MainActivity.java:76)
09-19 15:20:53.687: E/AndroidRuntime(25177): at com.example.project1.MainActivity$MyTask.onPostExecute(MainActivity.java:1)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.os.Looper.loop(Looper.java:137)
09-19 15:20:53.687: E/AndroidRuntime(25177): at android.app.ActivityThread.main(ActivityThread.java:4931)
09-19 15:20:53.687: E/AndroidRuntime(25177): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:20:53.687: E/AndroidRuntime(25177): at java.lang.reflect.Method.invoke(Method.java:511)
09-19 15:20:53.687: E/AndroidRuntime(25177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-19 15:20:53.687: E/AndroidRuntime(25177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-19 15:20:53.687: E/AndroidRuntime(25177): at dalvik.system.NativeStart.main(Native Method)
来源:
public class MainActivity extends Activity {
Context context;
ArrayList<String> aa = new ArrayList<String>();
ListView lv;
final String URL = "http://news.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.splash);
lv= (ListView) findViewById(R.id.listView1);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
aa.add(element.text().toString());
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
}
}
}