2015-06-14 96 views
-1

香港专业教育学院一直在关注这个帖子的Android DrawLayout实施

Android Navigation Drawer implemented with Activities

但是应用程序崩溃它显示任何内容之前。同时它不似乎得到了OnCreate中menthod作为其不会触发任何断点

这里是我的MainActivity

package com.example.alex.menutest2; 

import android.content.Intent; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 



public class MainActivity extends ActionBarActivity { 

    public ListView mLeftDrawerList = (ListView)findViewById(R.id.left_drawer); 
    public DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // do other stuff to initialize drawer layout, add list items 
     // ……… 
     // ………. 
     // add a listener to the drawer list view 
     mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    } 


    private class DrawerItemClickListener implements ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      switch (position) { 
       case 0: { 
        Intent intent = new Intent(MainActivity.this, DocumentActivity.class); 
        startActivity(intent); 
        break; 
       } 
       default: 
        break; 
      } 
      mDrawerLayout.closeDrawer(mLeftDrawerList); 
     } 
    } 
} 

这里是我的XML布局

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

    <FrameLayout 
     android:id="@+id/activity_frame"/> 

    <ListView 
    android:id="@+id/left_drawer" 
     android:entries="@array/menu_array"/> 
</android.support.v4.widget.DrawerLayout> 

这里是我的堆栈跟踪

06-14 19:14:26.127 8093-8093/com.example.alex.menutest2 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.alex.menutest2, PID: 8093 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alex.menutest2/com.example.alex.menutest2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
      at android.app.Activity.findViewById(Activity.java:2072) 
      at com.example.alex.menutest2.MainActivity.<init>(MainActivity.java:17) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1606) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1066) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5257) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+0

findViewById必须被称为fter setContentView()。 – Harry

+0

为什么要投票? – PowerMan2015

回答

1

我可能是错的,但我认为你不应该初始化你的全局标准调用onCreate()方法之前的ameter。你正试图在contentView被设置之前用findViewById()来初始化它们。

基于错误日志,你得在mDrawerLayout一个空指针,这让我猜猜下面一行也没找到一个观点:
public DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

+0

使完美的感觉。我会调整并让你知道我如何继续 – PowerMan2015

0

你应该找到的onCreate您的看法,后setContentView(R.layout.activity_main); 像这样:

public ListView mLeftDrawerList ; 
public DrawerLayout mDrawerLayout ; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mLeftDrawerList = (ListView)findViewById(R.id.left_drawer); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout) 
     // add a listener to the drawer list view 
     mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

} 

此外,我建议Butterknife LIB保持你的代码更干净

只有