OnCreate中的空指针错误

时间:2014-06-16 02:20:22

标签: android nullpointerexception oncreate

所以我在项目的第33行得到了Null Pointer错误,但我不知道它是什么。任何帮助将不胜感激。

    06-15 21:15:24.813: E/AndroidRuntime(15696): java.lang.RuntimeException: Unable to start activiy
ComponentInfo{yc.android.yourchallenger/yc.android.yourchallenger.MainActivity}: java.lang.NullPointerException
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.os.Looper.loop(Looper.java:137)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.main(ActivityThread.java:5493)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at java.lang.reflect.Method.invoke(Method.java:525)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at dalvik.system.NativeStart.main(Native Method)
06-15 21:15:24.813: E/AndroidRuntime(15696): Caused by: java.lang.NullPointerException
06-15 21:15:24.813: E/AndroidRuntime(15696):    at yc.android.yourchallenger.MainActivity.onCreate(MainActivity.java:33)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.Activity.performCreate(Activity.java:5372)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-15 21:15:24.813: E/AndroidRuntime(15696):    ... 11 more

以下是我收到错误的MainActivity         包yc.android.yourchallenger;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private String [] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;    
private CharSequence mDrawerTitle;
private CharSequence mTitle;

 public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.nav_drawer, mNavigationDrawerItemTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener((OnItemClickListener) new DrawerItemClickListener());
}

 private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);           
        }           
    }

 private void selectItem(int position) {
     Fragment fragment = new CreateFragment();
     Bundle args = new Bundle();
     args.putInt("key", position);
     fragment.setArguments(args);

     FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
     // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
 }   

 @Override
 public void setTitle(CharSequence title){
     mTitle = title;
     getActionBar().setTitle(mTitle);
 }


} 

2 个答案:

答案 0 :(得分:1)

确保您的活动的布局包含带有android:id="@+id/left_drawer"的ListView,否则对findViewById()的调用将返回null。

答案 1 :(得分:0)

正如我在评论中所建议的那样,请确保ListView内有activity_main layout名为 left_drawer 的内容。否则,您无法使用findViewById()调用它。

您没有向mNavigationDrawerItemTitles字符串数组添加任何值,而是将其添加到适配器中。也称为setOnItemClickListener()。但是列表中没有值。因为您的mNavigationDrawerItemTitles是空的。将一些值添加到字符串数组并进行测试。