Android NumberPicker NullPointer why?

I am trying to implement numberPicker for picking minute values.

But I am getting a NullPointer exception on this line:

minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);

The following code:

public class MainActivity extends Activity {


NumberPicker minutePicker;

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

    // Auswahl Minuten zum starten / Stoppen aller
    minutePicker = new NumberPicker(MainActivity.this);
    minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
    minutePicker.setMaxValue(30);


    minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            abschaltzeit = minutePicker.getValue();
        }
    });

    minutePicker.setValue(0);
    minutePicker.setWrapSelectorWheel(false);

}
}

      

XML:

<NumberPicker
    android:id="@+id/minuten_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="6"
    android:layout_column="0"
    android:paddingLeft="20dp" />

      

Log:

09-25 11:00:09.749  10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.carsten.awesome.app, PID: 10687
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

      

+3


source to share


2 answers


minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);

      

You create NumberPicker

programmatically and then overwrite the link when it returns findViewById()

. It returns null if your layout activity_main

doesn't contain minuten_picker

.



  • Choose just the other: either create it programmatically, or find it from the bloated view hierarchy.

  • If you choose programmatically new NumberPicker()

    , don't forget to add it to some layout in the activity hierarchy, for example. fromsetContentView()

  • If you chose inflation mode, make sure you have a view in your XML layout file.

I am assuming that the NPE you are seeing is actually on the next line where you are trying to call the method on minutePicker

and it is null.

+3


source


I'm sorry.

I moved the code from MainActivity in the creating fragment and it works with rootView.findView.



Sorry, but I'm new to Fragement Konzept.

Many thanks for your help!

0


source







All Articles