位置变量处的空指针异常

时间:2014-06-03 11:06:12

标签: android nullpointerexception stack-trace

我在HomeActivity.java中得到了空指针异常。我在该编码中指出了第54行。

StackTrace:

E/AndroidRuntime(3161): FATAL EXCEPTION: main
 E/AndroidRuntime(3161): Process: com.fth.android, PID: 3161
 E/AndroidRuntime(3161): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fth.android/com.sit.fth.activity.HomeActivity}: java.lang.NullPointerException
 E/AndroidRuntime(3161):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 E/AndroidRuntime(3161):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(3161):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(3161):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(3161):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(3161):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(3161):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(3161):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(3161):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(3161):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(3161):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(3161):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(3161): Caused by: java.lang.NullPointerException
 E/AndroidRuntime(3161):    at com.sit.fth.activity.HomeActivity.onCreate(HomeActivity.java:54)
 E/AndroidRuntime(3161):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(3161):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(3161):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(3161):    ... 11 more

HomeActivity.java:

package com.sit.fth.activity;

public class HomeActivity extends FragmentActivity{

    private ActionBar actionabar;
    private ViewPager viewpager;
    private LiveStreamFragment liveStreamFragment;
    private AppData appData;
    private FragmentManager fm;
    private MyFragmentPagerAdapter fragmentPagerAdapter;
    private int position;

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

        setContentView(R.layout.activity_main);
        position = getIntent().getExtras().getInt("position"); // 54 th Line Error
       appData = ((GemsApplication) this.getApplication()).getAppData();
      Log.v("position", position + "");

        actionabar = getActionBar();
        actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // actionabar.setHomeButtonEnabled(true);

        liveStreamFragment = new LiveStreamFragment();

        viewpager = (ViewPager) findViewById(R.id.pager);
        fm = getSupportFragmentManager();

        /** Defining a listener for pageChange */
        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                actionabar.setSelectedNavigationItem(position);
            }
        };

        /** Setting the pageChange listner to the viewPager */
        viewpager.setOnPageChangeListener(pageChangeListener);

        /** Creating an instance of FragmentPagerAdapter */
        fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

        viewpager.setAdapter(fragmentPagerAdapter);
        actionabar.setDisplayShowTitleEnabled(true);

} 

我不知道为什么错误显示在位置变量。任何人都可以帮我这些。谢谢。

3 个答案:

答案 0 :(得分:1)

你应该检查你的意图是否传递了额外内容,所以:

Bundle b = getIntent().getExtras();

if(b != null) {
    position = b.getInt("position");
}

如果b为null,您可能无法通过意图获取信息......

答案 1 :(得分:1)

getIntent().getExtras()返回先前使用putExtra()添加的所有额外内容的地图。如果之前未添加任何数据,则它将返回null。

首先检查您是否使用Activity密钥从其他FragmentHomeActivityposition发送任何数据。此外,您可以检查以下内容以避免崩溃,< / p>

Bundle bundle=getIntent().getExtras();
     if(bundle!=null) {
             position = bundle.getInt("position"); 
        }

答案 2 :(得分:0)

在获取Integer之前,您必须检查您正在访问的Bundle对象是否为null。 还要确保键“位置”相同。我建议您使用变量代替硬编码字符串。

好看。

编辑:

见佐兰的答案。