0

I am playing around with using a ListView with an Navigation Drawer. Eclipse does not give me any errors but when i start the code in the emulator it says: "Unfortunately, DrunKingz has stopped."

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{
 

 private DrawerLayout DrawerLayout;
 private ActionBarDrawerToggle ActionBarDrawerToggle;
 
 public ListView drawerlist;
 public String[] listentext = {"DrunKingz","Songs","Kontakt"};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);

  DrawerLayout = (DrawerLayout) findViewById(R.id.dlDrawerLayout);
  ActionBarDrawerToggle = new ActionBarDrawerToggle(this, DrawerLayout, R.drawable.icon_drawer, R.string.open, R.string.close);
  DrawerLayout.setDrawerListener(ActionBarDrawerToggle);

  getActionBar().setDisplayHomeAsUpEnabled(true);
  getActionBar().setHomeButtonEnabled(true);
  
  // ListView
  // ------
  // ------
  
  drawerlist = (ListView) findViewById(R.id.drawerliste);
  ArrayAdapter<String> drawerlistadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listentext);
  drawerlist.setAdapter(drawerlistadapter);
  drawerlist.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    
    switch (drawerlist.getPositionForView(view)) {
    case 0: {
     Toast.makeText(getApplicationContext(), "DrunKingz", Toast.LENGTH_SHORT).show();
     break;
      }
    
    case 1: {
     Toast.makeText(getApplicationContext(), "Songs", Toast.LENGTH_SHORT).show();
     break;
      }
    
    case 2: {
     Toast.makeText(getApplicationContext(), "Kontakt", Toast.LENGTH_SHORT).show();
     break;
      }
     

    }
    
   }
  });
  
 }

 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
  super.onPostCreate(savedInstanceState);

  ActionBarDrawerToggle.syncState();
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  ActionBarDrawerToggle.onConfigurationChanged(newConfig);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  if (ActionBarDrawerToggle.onOptionsItemSelected(item)) {
   return true;
  }

  return super.onOptionsItemSelected(item);
 }

 @Override
 public void onClick(View v) {
  Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
 }

}

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):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:137)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 21:37:02.920: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 21:37:02.920: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:511)
09-20 21:37:02.920: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 21:37:02.920: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 21:37:02.920: E/AndroidRuntime(827):  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):  at de.pascalborker.drunkingz.MainActivity.onCreate(MainActivity.java:33)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.Activity.performCreate(Activity.java:4465)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 21:37:02.920: E/AndroidRuntime(827):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-20 21:37:02.920: E/AndroidRuntime(827):  ... 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):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:250)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl.getNetworkStatsDetailGroupedByUid(BatteryStatsImpl.java:5734)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl.access$100(BatteryStatsImpl.java:76)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl$Uid.computeCurrentTcpBytesReceived(BatteryStatsImpl.java:2457)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl$Uid.getTcpBytesReceived(BatteryStatsImpl.java:2446)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl.writeSummaryToParcel(BatteryStatsImpl.java:5437)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl.writeLocked(BatteryStatsImpl.java:4836)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.os.BatteryStatsImpl.writeAsyncLocked(BatteryStatsImpl.java:4818)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.server.am.ActivityManagerService.updateCpuStatsNow(ActivityManagerService.java:1652)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  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):  at libcore.io.IoBridge.open(IoBridge.java:406)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at java.io.FileInputStream.<init>(FileInputStream.java:78)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  ... 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):  at libcore.io.Posix.open(Native Method)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  at libcore.io.IoBridge.open(IoBridge.java:390)
09-20 21:39:05.640: A/BatteryStatsImpl(77):  ... 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):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.os.Looper.loop(Looper.java:137)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 21:48:39.280: E/AndroidRuntime(868):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 21:48:39.280: E/AndroidRuntime(868):  at java.lang.reflect.Method.invoke(Method.java:511)
09-20 21:48:39.280: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 21:48:39.280: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 21:48:39.280: E/AndroidRuntime(868):  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):  at de.pascalborker.drunkingz.MainActivity.onCreate(MainActivity.java:33)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.Activity.performCreate(Activity.java:4465)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 21:48:39.280: E/AndroidRuntime(868):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-20 21:48:39.280: E/AndroidRuntime(868):  ... 11 more

Anyone know what might be causing this?

jww
  • 97,681
  • 90
  • 411
  • 885
  • Why is `setContentView(R.layout.activity_main);` commented out? You need that. – Mike M. Sep 20 '14 at 21:59
  • Since you are playing around: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). I'm taking "playing around" to mean you are learning it. – jww Sep 20 '14 at 22:23

1 Answers1

1

In your onCreate()

//setContentView(R.layout.activity_main);

DrawerLayout = (DrawerLayout) findViewById(R.id.dlDrawerLayout);

if you did not set the content View with that layout, then findViewById will return null, and

DrawerLayout.setDrawerListener(ActionBarDrawerToggle);

that will throw the NullPointerException.

uncomment that line

El Abogato
  • 123
  • 1
  • 5
  • I think that solved the problem. but i have another one: LogCat is telling me "Your content must have a ListView whose id attribute is 'android.R.id.list'. What does that mean? Sorry im completely new in this area. – thetigerhay Sep 20 '14 at 23:34
  • @thetigerhay A ListActivity expects a certain layout setup. You probably just want a regular Activity. That is, `MainActivity extends Activity`. – Mike M. Sep 20 '14 at 23:43
  • That means that your layout needs a ListView Widget, with an id attribute set to: android.R.id.list. Why that? Because you are extending ListViewActivity instead of a normal Activity class. ListViewActivity expect to have a list view inside with the id pointed before. I see in your activity_main.xml layout that no list view is added. AS Mike said, just change the parent Activity class, or add the ListView if you really need it. – El Abogato Sep 21 '14 at 13:25