Nullpointer异常 - FragmentActivity

时间:2014-11-04 16:08:16

标签: java android android-layout android-activity android-fragments

我的片段活动中收到了一个nullpointer异常错误。

以下是日志猫:

11-04 06:29:51.550: E/AndroidRuntime(19286): FATAL EXCEPTION: main
11-04 06:29:51.550: E/AndroidRuntime(19286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MainActivity}: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.os.Looper.loop(Looper.java:137)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at dalvik.system.NativeStart.main(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286): Caused by: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.dooba.beta.MainActivity.onCreate(MainActivity.java:51)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.Activity.performCreate(Activity.java:5008)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-04 06:29:51.550: E/AndroidRuntime(19286):    ... 11 more

我相信错误发生在以下几行:

 mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
         .getThemedContext(), android.R.layout.simple_list_item_1,
         mOptionMenu));

以下是整个活动代码:

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    private String[] mOptionMenu;
    private DrawerLayout mDrawerLayout;
    private RelativeLayout mDrawerRelativeLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mTitleSection;
    private CharSequence mTitleApp;

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



        mOptionMenu = new String[] { "Opción 1", "Opción 2", "Opción 3" };
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         mDrawerRelativeLayout = (RelativeLayout)
         findViewById(R.id.left_drawer);
         mDrawerList = (ListView) findViewById(R.id.list_view_drawer);
         mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
         .getThemedContext(), android.R.layout.simple_list_item_1,
         mOptionMenu));

         mDrawerList.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {

         Fragment fragment = null;

         switch (position) {
         case 0:
         fragment = new FirstFragment();
         break;
         case 1:
         fragment = new SecondFragment();
         break;
         case 2:
         fragment = new ThirdFragment();
         break;
         }

         FragmentManager fragmentManager = getSupportFragmentManager();

         fragmentManager.beginTransaction()
         .replace(R.id.content_frame, fragment).commit();

         mDrawerList.setItemChecked(position, true);

         mTitleSection = mOptionMenu[position];
         getActionBar().setTitle(mTitleSection);

         mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
         }
         });
         mDrawerList.setItemChecked(0, true);
         mTitleSection = getTitle();
         mTitleApp = getTitle();

         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
         R.drawable.ic_drawer, R.string.drawer_open,
         R.string.drawer_close) {

         public void onDrawerClosed(View view) {
         getActionBar().setTitle(mTitleSection);
         ActivityCompat.invalidateOptionsMenu(MainActivity.this);
         }

         public void onDrawerOpened(View drawerView) {
         getActionBar().setTitle(mTitleSection);
         ActivityCompat.invalidateOptionsMenu(MainActivity.this);
         }
         };

         mDrawerLayout.setDrawerListener(mDrawerToggle);

         getActionBar().setDisplayHomeAsUpEnabled(true);
         getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

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

        switch (item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
            ;
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

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

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

如果需要澄清,请告诉我。 提前谢谢。

2 个答案:

答案 0 :(得分:3)

由于你要导入import android.support.v7.app.ActionBarActivity,我想你应该使用

getSupportActionBar()

而不是

getActionBar()

答案 1 :(得分:1)

而不是

getActionBar().getThemedContext()

使用this