创建单独的活动时出现空指针异常

时间:2014-06-02 11:39:03

标签: android nullpointerexception android-activity

在创建名为GalleryActivity的单独活动期间,我得到一个空指针运行时异常。

堆栈跟踪:

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

GalleryActivity.java:

package com.sit.fth.activity;

public class GalleryActivity extends FragmentActivity {

    private long enqueue;
    private DownloadManager dm;

    private ActionBar actionabar;
    private ViewPager viewpager;

    private AppData appData;
    private FragmentManager fm;
    private MyFragmentPagerAdapter fragmentPagerAdapter;
    private int position;
    private Video video;
    private DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    private ViewPager mViewPager;
    private static String id;
    private static String name;



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


            position = getIntent().getExtras().getInt("position");

            id = getIntent().getExtras().getString("id");

            name = getIntent().getExtras().getString("name");



            mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());


            final ActionBar actionBar = getActionBar();


            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mDemoCollectionPagerAdapter);  // 77th Line Error
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:

                    Intent upIntent = new Intent(this, HomeActivity.class);
                    upIntent.putExtra("position", position);
                    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {

                        TaskStackBuilder.from(this)
                                // If there are ancestor activities, they should be added here.
                                .addNextIntent(upIntent)
                                .startActivities();
                        finish();
                    } else {
                        // This activity is part of the application's task, so simply
                        // navigate up to the hierarchical parent activity.
                        NavUtils.navigateUpTo(this, upIntent);
                    }
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment
         * representing an object in the collection.
         */
        public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {

            public DemoCollectionPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int i) {


            //  VideoFragment videoFragment = new VideoFragment();



                GalleryDetailFragment galleryDetailFragment = new GalleryDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString("id", id);
                bundle.putString("name", name);
                galleryDetailFragment.setArguments(bundle);


                return galleryDetailFragment;
            }

            @Override
            public int getCount() {
                // For this contrived example, we have a 100-object collection.
                return 1;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "OBJECT " + (position + 1);
            }
        }


        public static class DemoObjectFragment extends Fragment {

            public static final String ARG_OBJECT = "object";

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
                Bundle args = getArguments();
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                        Integer.toString(args.getInt(ARG_OBJECT)));
                return rootView;
            }
        }
    }  

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fth.android"
    android:versionCode="1"
    android:versionName="1.0" >
  <application
        android:name="com.sit.fth.app.GemsApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
         android:theme="@android:style/Theme.Holo.Light" >

        <activity
            android:name="com.sit.fth.activity.SplashActivity"
            android:parentActivityName="com.sit.fth.activity.HomeActivity" 
            android:label="@string/app_name"
            android:screenOrientation="portrait" 
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">


            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.sit.fth.activity.HomeActivity"
            android:screenOrientation="portrait" >
             <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.sit.fth.activity.HomeActivity" />

        </activity>

        <activity android:name="com.sit.fth.activity.YoutubePlayActivity" >
        </activity>

        <activity android:name="com.sit.fth.activity.GalleryActivity">

        </activity>


    </application>

</manifest>  

任何人都可以帮助我。感谢你。

3 个答案:

答案 0 :(得分:2)

在尝试查找视图之前,您没有调用setContentView

mViewPager = (ViewPager) findViewById(R.id.pager);  

需要确定您尝试查找mViewPager的布局。

在任何setContentView(R.layout.yourlayoutname);之前调用onCreate()中的findViewById

答案 1 :(得分:1)

您的活动中缺少setContentView。您需要先将布局内容设置为活动,然后初始化视图。

您的mViewPager为空,并且您正在通过NullPointerException的空值调用setAdapter。

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
    ...// rest of the code

答案 2 :(得分:1)

您必须在活动中初始化布局,这就是您收到错误的原因

setContentView(Your layout);