我正在玩與使用導航抽屜ListView。 Eclipse不會給我任何錯誤,但是當我在模擬器中啓動代碼時,它會說:「不幸的是,DrunKingz已經停止了。」NullPointerException與ListView
MainActivity.java:
package de.pascalborker.drunkingz;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity implements OnClickListener{
\t
\t private DrawerLayout DrawerLayout;
\t private ActionBarDrawerToggle ActionBarDrawerToggle;
\t
\t public ListView drawerlist;
\t public String[] listentext = {"DrunKingz","Songs","Kontakt"};
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t super.onCreate(savedInstanceState);
\t \t //setContentView(R.layout.activity_main);
\t \t DrawerLayout = (DrawerLayout) findViewById(R.id.dlDrawerLayout);
\t \t ActionBarDrawerToggle = new ActionBarDrawerToggle(this, DrawerLayout, R.drawable.icon_drawer, R.string.open, R.string.close);
\t \t DrawerLayout.setDrawerListener(ActionBarDrawerToggle);
\t \t getActionBar().setDisplayHomeAsUpEnabled(true);
\t \t getActionBar().setHomeButtonEnabled(true);
\t \t
\t \t // ListView
\t \t // ------
\t \t // ------
\t \t
\t \t drawerlist = (ListView) findViewById(R.id.drawerliste);
\t \t ArrayAdapter<String> drawerlistadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listentext);
\t \t drawerlist.setAdapter(drawerlistadapter);
\t \t drawerlist.setOnItemClickListener(new OnItemClickListener() {
\t \t \t @Override
\t \t \t public void onItemClick(AdapterView<?> parent, View view,
\t \t \t \t \t int position, long id) {
\t \t \t \t
\t \t \t \t switch (drawerlist.getPositionForView(view)) {
\t \t \t \t case 0: {
\t \t \t \t \t Toast.makeText(getApplicationContext(), "DrunKingz", Toast.LENGTH_SHORT).show();
\t \t \t \t \t break;
\t \t \t \t \t \t }
\t \t \t \t
\t \t \t \t case 1: {
\t \t \t \t \t Toast.makeText(getApplicationContext(), "Songs", Toast.LENGTH_SHORT).show();
\t \t \t \t \t break;
\t \t \t \t \t \t }
\t \t \t \t
\t \t \t \t case 2: {
\t \t \t \t \t Toast.makeText(getApplicationContext(), "Kontakt", Toast.LENGTH_SHORT).show();
\t \t \t \t \t break;
\t \t \t \t \t \t }
\t \t \t \t \t
\t \t \t \t }
\t \t \t \t
\t \t \t }
\t \t });
\t \t
\t }
\t @Override
\t protected void onPostCreate(Bundle savedInstanceState) {
\t \t super.onPostCreate(savedInstanceState);
\t \t ActionBarDrawerToggle.syncState();
\t }
\t @Override
\t public void onConfigurationChanged(Configuration newConfig) {
\t \t super.onConfigurationChanged(newConfig);
\t \t ActionBarDrawerToggle.onConfigurationChanged(newConfig);
\t }
\t @Override
\t public boolean onOptionsItemSelected(MenuItem item) {
\t \t if (ActionBarDrawerToggle.onOptionsItemSelected(item)) {
\t \t \t return true;
\t \t }
\t \t return super.onOptionsItemSelected(item);
\t }
\t @Override
\t public void onClick(View v) {
\t \t Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
\t }
}
activity_main.xml中:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dlDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Content -->
<RelativeLayout
android:id="@+id/rlContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hauptansicht"
android:gravity="center"/>
</RelativeLayout>
<!-- Menu -->
<RelativeLayout
android:id="@+id/rlDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/menuansicht"
android:gravity="center"/>
<ListView
android:id="@+id/drawerliste"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
logcat的:
09-20 21:37:02.920: E/AndroidRuntime(827): FATAL EXCEPTION: main
09-20 21:37:02.920: E/AndroidRuntime(827): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.pascalborker.drunkingz/de.pascalborker.drunkingz.MainActivity}: java.lang.NullPointerException
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.os.Looper.loop(Looper.java:137)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at java.lang.reflect.Method.invokeNative(Native Method)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at java.lang.reflect.Method.invoke(Method.java:511)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at dalvik.system.NativeStart.main(Native Method)
09-20 21:37:02.920: E/AndroidRuntime(827): Caused by: java.lang.NullPointerException
09-20 21:37:02.920: E/AndroidRuntime(827): \t at de.pascalborker.drunkingz.MainActivity.onCreate(MainActivity.java:33)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.Activity.performCreate(Activity.java:4465)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 21:37:02.920: E/AndroidRuntime(827): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-20 21:37:02.920: E/AndroidRuntime(827): \t ... 11 more
09-20 21:39:05.640: A/BatteryStatsImpl(77): problem reading network stats
09-20 21:39:05.640: A/BatteryStatsImpl(77): java.lang.IllegalStateException: problem parsing idx 1
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:250)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl.getNetworkStatsDetailGroupedByUid(BatteryStatsImpl.java:5734)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl.access$100(BatteryStatsImpl.java:76)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl$Uid.computeCurrentTcpBytesReceived(BatteryStatsImpl.java:2457)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl$Uid.getTcpBytesReceived(BatteryStatsImpl.java:2446)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl.writeSummaryToParcel(BatteryStatsImpl.java:5437)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl.writeLocked(BatteryStatsImpl.java:4836)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.os.BatteryStatsImpl.writeAsyncLocked(BatteryStatsImpl.java:4818)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.server.am.ActivityManagerService.updateCpuStatsNow(ActivityManagerService.java:1652)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.server.am.ActivityManagerService$3.run(ActivityManagerService.java:1534)
09-20 21:39:05.640: A/BatteryStatsImpl(77): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at libcore.io.IoBridge.open(IoBridge.java:406)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at java.io.FileInputStream.<init>(FileInputStream.java:78)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t ... 10 more
09-20 21:39:05.640: A/BatteryStatsImpl(77): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at libcore.io.Posix.open(Native Method)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t at libcore.io.IoBridge.open(IoBridge.java:390)
09-20 21:39:05.640: A/BatteryStatsImpl(77): \t ... 12 more
09-20 21:48:39.280: E/AndroidRuntime(868): FATAL EXCEPTION: main
09-20 21:48:39.280: E/AndroidRuntime(868): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.pascalborker.drunkingz/de.pascalborker.drunkingz.MainActivity}: java.lang.NullPointerException
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.os.Looper.loop(Looper.java:137)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at java.lang.reflect.Method.invokeNative(Native Method)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at java.lang.reflect.Method.invoke(Method.java:511)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at dalvik.system.NativeStart.main(Native Method)
09-20 21:48:39.280: E/AndroidRuntime(868): Caused by: java.lang.NullPointerException
09-20 21:48:39.280: E/AndroidRuntime(868): \t at de.pascalborker.drunkingz.MainActivity.onCreate(MainActivity.java:33)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.Activity.performCreate(Activity.java:4465)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 21:48:39.280: E/AndroidRuntime(868): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-20 21:48:39.280: E/AndroidRuntime(868): \t ... 11 more
任何人都知道可能是什麼原因造成的?
爲什麼'setContentView(R.layout.activity_main);'註釋掉了?你需要這個。 – 2014-09-20 21:59:27
由於你在玩耍:[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-我如何解決它)。我正在「玩耍」意味着你正在學習它。 – jww 2014-09-20 22:23:16