简体   繁体   中英

Android: NullPointerException occurs with ViewPager and PagerAdapter

I am writing an application with NumberPickers. The number pickers are fine when none of their methods are used, but when i call the NumberPicker.setMaxValue method, a NullPointer Exception occurs, and the app fails to start. No errors occur without the usage of a set max ot set min method. Update: I think this error could have been caused by my using of the PagerAdapter and ViewPager, as the NumberPicker in question is not on the first page. Here is my onCreate Method:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //All the pager stuff
 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.pager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(0);
 //Start of real code.
 final NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);
 //Code responsible for error follows
 LGmade.setMaxValue(3);
 LGmade.setMinValue(0);
 }

Here is the XML:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/bc"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Low Goals"
        android:id="@+id/textView"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp" />

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LGmade"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_marginTop="10dp"
        android:showDividers="middle"
        android:orientation="vertical"/>

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LGmissed"
        android:layout_row="3"
        android:layout_column="2"
        android:layout_marginTop="10dp"
        android:showDividers="middle"
        android:orientation="vertical"/>

</GridLayout>

Here is the logcat:

03-12 00:24:56.944    1334-1334/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lmrobotics.frcgamescout.app/com.lmrobotics.frcgamescout.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.lmrobotics.frcgamescout.app.MainActivity.onCreate(MainActivity.java:55)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

With the information provided I would say that

R.id.LGmade

is probably not the correct id, and therefore the var LGMade is null.

hey der ur trying to initialize a final variable NumberPicker LGmade in onCreate method, this is not allowed. Well u can simple remove the final keyword or u can initialize it in the constructor.

NOTE: 
A final variable can only be initialized once, either via an 
      initializer or an assignment statement. 
It does not need to be initialized at the point of declaration: this is called a 
      "blank final" variable. 
A blank final instance variable of a class must be definitely assigned in every 
      constructor of class in which it is declared

change below

final NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);

to

NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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