添加导航抽屉后,Android应用程序崩溃

时间:2015-02-01 11:46:03

标签: java android xml listview navigation-drawer

我正在使用这个Android应用程序。在这个应用程序中,“StartingActivity.java”自动变为“HomeActivity.java”,其中包含导航抽屉。但在更改活动时,应用程序崩溃。请帮助.. 这是代码

StartingActivity.java

 package com.example.abhi.mymanager;
 import android.content.Intent;
 import android.os.Handler;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;

 public class StartingActivity extends ActionBarActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_starting);
  // for sending it to another activity after a time
    Handler handler=new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
          Intent i=new Intent(StartingActivity.this,HomeActivity.class);
          startActivity(i);
          }
        }
    },5000);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is   present.
    getMenuInflater().inflate(R.menu.menu_starting, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

HomeActivity.java

package com.example.abhi.mymanager;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

 /**
 * Created by abhi on 29-01-2015.
 */
public class HomeActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        listView=(ListView)findViewById(R.id.left_drawer);
        listView.setAdapter(new ArrayAdapter<>   (this,android.R.layout.simple_list_item_1,options));
        listView.setOnItemClickListener(this);



    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,options[position],Toast.LENGTH_LONG).show();
        getSupportActionBar().setTitle(options[position]);
    }
}

activity_starting.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"
    android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"  tools:context=".StartingActivity"
    android:background="#ffb6a6ff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="My Manager"
        android:id="@+id/textView"
        android:layout_marginTop="65dp"
        android:textSize="@dimen/abc_action_bar_default_height_material"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ff2312ff" />
</RelativeLayout>

home_layout.xml

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <!-- the main content view-->
        <FrameLayout
           android:id="@+id/content_frame"
            android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- the navigation drawer-->
    <ListView
        android:id="@+id/left_drawer"
        android:entries="@array/Options"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@android:color/holo_purple"

        />
     </android.support.v4.widget.DrawerLayout>

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abhi.mymanager" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".StartingActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HomeActivity"
            android:label="@string/app_name"
            >
            </activity>

    </application>

</manifest>

的strings.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">My Manager</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="Options">
        <item name="MyBoard">My Board</item>
        <item name="CreateBoard">Create Board</item>
    </string-array>


</resources>

logcat的

02-01 17:23:55.811  13075-13082/com.example.abhi.mymanager I/jdwp﹕      Ignoring second debugger -- accepting and dropping
02-01 17:23:56.171  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method   android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 11346: Landroid/view /ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 11354: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0008
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 9040: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 11349: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 365: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-01 17:23:56.181  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-01 17:23:56.191  13075-13075/com.example.abhi.mymanager I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-01 17:23:56.191  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ VFY: unable to resolve virtual method 387: Landroid/content/res/TypedArray;.getType (I)I
02-01 17:23:56.191  13075-13075/com.example.abhi.mymanager D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-01 17:23:58.851  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:23:59.561  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:23:59.911  13075-13075/com.example.abhi.mymanager I/PGA﹕ New SOCKET connection: .abhi.mymanager (pid 13075, tid 13075)
02-01 17:24:00.081  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:24:01.661  13075-13075/com.example.abhi.mymanager D/AndroidRuntime﹕ Shutting down VM
02-01 17:24:01.661  13075-13075/com.example.abhi.mymanager W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2c9e180)
02-01 17:24:01.661  13075-13075/com.example.abhi.mymanager D/AndroidRuntime﹕ procName from cmdline: com.example.abhi.mymanager
02-01 17:24:01.661  13075-13075/com.example.abhi.mymanager E/AndroidRuntime﹕ in writeCrashedAppName, pkgName  :com.example.abhi.mymanager
02-01 17:24:01.671  13075-13075/com.example.abhi.mymanager D/AndroidRuntime﹕ file written successfully with content: com.example.abhi.mymanager StringBuffer : ;com.example.abhi.mymanager
02-01 17:24:01.681  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:24:02.081  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:24:02.531  13075-13082/com.example.abhi.mymanager I/jdwp﹕ Ignoring second debugger -- accepting and dropping
02-01 17:24:03.021  13075-13075/com.example.abhi.mymanager I/Process﹕ Sending signal. PID: 13075 SIG: 9
02-01 17:24:03.021  13075-13075/com.example.abhi.mymanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abhi.mymanager/com.example.abhi.mymanager.HomeActivity}: java.lang.NullPointerException
                at             android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                at android.app.ActivityThread.access$600(ActivityThread.java:123)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4424)
                 at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)

       Caused by: java.lang.NullPointerException
        at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
        at java.util.Arrays.asList(Arrays.java:154)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
        at com.example.abhi.mymanager.HomeActivity.onCreate(HomeActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
         at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您的options数组未初始化。您无法将null作为数组传递给ArrayAdapter