简体   繁体   English

为导航抽屉扩展BaseActivity的问题

[英]Issue with extending BaseActivity for navigation drawer

I have looked around SO as well as Google searches, and have decided that my activities extending a BaseActivity was the best way to integrate a navigation drawer in my project. 我一直在研究SO和Google搜索,并认为扩展BaseActivity的活动是在项目中集成导航抽屉的最佳方法。 Whenever I start my app though, it crashes immediately. 但是,无论何时启动我的应用程序,它都会立即崩溃。 Below I have included my BaseActivity code and the issue lies with this line: 下面,我包括了我的BaseActivity代码,问题在于此行:

    drawerLayout.setDrawerListener(drawerToggle);

It results in a NullPointerException 结果为NullPointerException

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;
    Intent twitch = new Intent(this, TwitchActivity.class);
    Intent community = new Intent(this, CommunityActivity.class);
    Intent esports = new Intent(this, ESportsActivity.class);
    Intent home = new Intent(this, MainActivity.class);

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.id.drawer_layout);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        final ActionBar actionBar = getActionBar();

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
        {
            public void onDrawerClosed(View view)
            {
                actionBar.setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView)
            {
                actionBar.setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);


        layers = getResources().getStringArray(R.array.layers);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
                layers));


        drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                String selected = arg0.getItemAtPosition(pos).toString();
                if(selected.equals("Twitch"))
                    startActivity(twitch);
                if(selected.equals("Community"))
                    startActivity(community);
                if(selected.equals("ESports"))
                    startActivity(esports);
                if(selected.equals("Home"))
                    startActivity(home);
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

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

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

My MainActivity 我的主要活动

public class MainActivity extends BaseActivity {

private Spinner spinner;
public static String region;
public static String name;

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

My drawer_layout.xml 我的drawer_layout.xml

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

<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">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav Drawer -->
    <ListView android:id="@+id/left_drawer"
        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="#111111"/>
</android.support.v4.widget.DrawerLayout>

Full Error: 完全错误:

04-10 20:17:25.409    4548-4548/edu.appdesign.leaguestats E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: edu.appdesign.leaguestats, PID: 4548
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.appdesign.leaguestats/edu.appdesign.leaguestats.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
            at android.content.res.Resources.loadXmlResourceParser(Resources.java)
            at android.content.res.Resources.getLayout(Resources.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.setContentView(Activity.java)
            at edu.appdesign.leaguestats.BaseActivity.onCreate(BaseActivity.java:34)
            at edu.appdesign.leaguestats.MainActivity.onCreate(MainActivity.java:42)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)

Your onCreate() must call setContentView() to set the layout before it calls findViewById() to get a view from within that layout. 您的onCreate()必须调用setContentView()来设置布局, 然后才能调用findViewById()从该布局中获取视图。 And make sure that you pass to setContentView() the proper layout resource (it should be something like 'R.layout.xxxxx', where 'xxxxx' is the name of your XML layout file, without the '.xml' extension). 并确保将正确的布局资源传递给setContentView() (它应该类似于“ R.layout.xxxxx”,其中“ xxxxx”是XML布局文件的名称,不带“ .xml”扩展名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM