我正在处理无法在列表项单击时开始新活动的应用程序。当我单击列表项时,应用程序崩溃并且我找不到错误。活动的代码是
public class CourierActivity extends Activity {
DBController dbc=new DBController(this);
Cursor c;
SimpleCursorAdapter sca;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.courierlist);
final ListView courierlists=(ListView) findViewById(R.id.courierview);
dbc.open();
c=dbc.getcouriername();
String [] from ={"couriernames"};
int [] to={R.id.couriertext};
sca=new SimpleCursorAdapter(this, R.layout.courierlistrow, c, from, to);
courierlists.setAdapter(sca);
sca.notifyDataSetChanged();
c.requery();
courierlists.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i=new Intent(CourierActivity.this,ServiceActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
}
}
这是我的 logcat 输出:
08-24 07:03:54.103: E/AndroidRuntime(1377): FATAL EXCEPTION: main
08-24 07:03:54.103: E/AndroidRuntime(1377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datab/com.example.datab.ServiceActivity}: java.lang.NullPointerException
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.os.Looper.loop(Looper.java:137)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 07:03:54.103: E/AndroidRuntime(1377): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 07:03:54.103: E/AndroidRuntime(1377): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 07:03:54.103: E/AndroidRuntime(1377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 07:03:54.103: E/AndroidRuntime(1377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 07:03:54.103: E/AndroidRuntime(1377): at dalvik.system.NativeStart.main(Native Method)
08-24 07:03:54.103: E/AndroidRuntime(1377): Caused by: java.lang.NullPointerException
08-24 07:03:54.103: E/AndroidRuntime(1377): at com.example.datab.ServiceActivity.onCreate(ServiceActivity.java:34)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.Activity.performCreate(Activity.java:5104)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 07:03:54.103: E/AndroidRuntime(1377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 07:03:54.103: E/AndroidRuntime(1377): ... 11 more