Pregunta

Based on the title, I am trying to develop a timer app that will give the user a dialog box with 2 number pickers. These 2 number pickers are for minutes and seconds and are set to max value 59 and min value 0. That being said the issue that I am having is that the setMaxValue for the first numberpicker is returning a null pointer error. Please see the portion of code and the log that it is returning:

        public void showDialog(){
    final Dialog cusd = new Dialog(MainActivity.this);
    cusd.setTitle("Tag and Timer Selector");
    cusd.setContentView(R.layout.dialogbox);
    final Button canlbtn = (Button) findViewById(R.id.cancelbtn);
    final Button sbtbtn = (Button) findViewById(R.id.submitbtn);
    final NumberPicker minnp = (NumberPicker) findViewById(R.id.numberPicker1);
    final NumberPicker secnp = (NumberPicker) findViewById(R.id.numberPicker2);
    final EditText tagvalue = (EditText) findViewById(R.id.tagname);
    minnp.setMaxValue(59);
    minnp.setMinValue(0);
    minnp.setWrapSelectorWheel(false);
    minnp.setOnValueChangedListener(this);
    secnp.setMaxValue(59);
    secnp.setMinValue(0);
    secnp.setWrapSelectorWheel(false);
    secnp.setOnValueChangedListener(this);
    canlbtn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            cusd.dismiss();

        }

    });
    sbtbtn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            tgview.setText(String.valueOf(tagvalue.getText()));
            minview.setText(String.valueOf(minnp.getValue()));
            secview.setText(String.valueOf(secnp.getValue()));
            cusd.dismiss();
        }

    });
    cusd.show();
}

The logs highlight that the error or null pointer exception is showing up at line 80 called from line 59 which is a call to the method from a switch case. Please see the logs below:

    01-01 16:29:49.982: W/dalvikvm(1946): threadid=1: thread exiting with uncaught exception (group=0xb4a57ba8)
    01-01 16:29:50.002: E/AndroidRuntime(1946): FATAL EXCEPTION: main
    01-01 16:29:50.002: E/AndroidRuntime(1946): Process: com.vertygoeclypse.multitimer, PID: 1946
    01-01 16:29:50.002: E/AndroidRuntime(1946): java.lang.NullPointerException
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at com.vertygoeclypse.multitimer.MainActivity.showDialog(MainActivity.java:80)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at com.vertygoeclypse.multitimer.MainActivity.onClick(MainActivity.java:59)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.view.View.performClick(View.java:4438)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.view.View$PerformClick.run(View.java:18422)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.os.Handler.handleCallback(Handler.java:733)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.os.Handler.dispatchMessage(Handler.java:95)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.os.Looper.loop(Looper.java:136)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invoke(Method.java:515)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    01-01 16:29:50.002: E/AndroidRuntime(1946):     at dalvik.system.NativeStart.main(Native Method)
    01-01 16:29:52.352: I/Process(1946): Sending signal. PID: 1946 SIG: 9

That being said the line 80 is the following:

            minnp.setMaxValue(59);

and I would like to find out why this is giving me the error because the lines following it:

            minnp.setMinValue(0);
    minnp.setWrapSelectorWheel(false);
    minnp.setOnValueChangedListener(this);
    secnp.setMaxValue(59);
    secnp.setMinValue(0);
    secnp.setWrapSelectorWheel(false);
    secnp.setOnValueChangedListener(this);

I believe will throw the same exception out. Please also see the below all my code and xml files as it relates to the app so far:

    package com.vertygoeclypse.multitimer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Dialog;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.NumberPicker;
    import android.widget.TextView;
    public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener, OnClickListener{
Button dgbtn, abbtn, exbtn, cvbtn;
TextView tgview, minview, secview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dgbtn = (Button) findViewById(R.id.dialogbtn);
    abbtn = (Button) findViewById(R.id.aboutbtn);
    exbtn = (Button) findViewById(R.id.exitbtn);
    cvbtn = (Button) findViewById(R.id.clrvaluesbtn);
    tgview = (TextView) findViewById(R.id.tagview);
    minview = (TextView) findViewById(R.id.minview);
    secview = (TextView) findViewById(R.id.seecview);
    dgbtn.setOnClickListener(this);
    abbtn.setOnClickListener(this);
    exbtn.setOnClickListener(this);
    cvbtn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    // TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.dialogbtn:
        showDialog();
        break;
    case R.id.exitbtn:
        finish();
    case R.id.clrvaluesbtn:
        tgview.setText("");
        minview.setText("");
        secview.setText("");
        break;
    }
}
public void showDialog(){
    final Dialog cusd = new Dialog(MainActivity.this);
    cusd.setTitle("Tag and Timer Selector");
    cusd.setContentView(R.layout.dialogbox);
    final Button canlbtn = (Button) findViewById(R.id.cancelbtn);
    final Button sbtbtn = (Button) findViewById(R.id.submitbtn);
    final NumberPicker minnp = (NumberPicker) findViewById(R.id.numberPicker1);
    final NumberPicker secnp = (NumberPicker) findViewById(R.id.numberPicker2);
    final EditText tagvalue = (EditText) findViewById(R.id.tagname);
    minnp.setMaxValue(59);
    minnp.setMinValue(0);
    minnp.setWrapSelectorWheel(false);
    minnp.setOnValueChangedListener(this);
    secnp.setMaxValue(59);
    secnp.setMinValue(0);
    secnp.setWrapSelectorWheel(false);
    secnp.setOnValueChangedListener(this);
    canlbtn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            cusd.dismiss();
        }
    });
    sbtbtn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            tgview.setText(String.valueOf(tagvalue.getText()));
            minview.setText(String.valueOf(minnp.getValue()));
            secview.setText(String.valueOf(secnp.getValue()));
            cusd.dismiss();
        }
    });
    cusd.show();
}
    }

Here are the xmls for the app, I will start with the main which is just a basic layout to holder the textviews and buttons, once the app is functional I will work on the looks:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Minutes" />

<TextView
    android:id="@+id/minview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Seconds" />

<TextView
    android:id="@+id/seecview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tag Name" />

<TextView
    android:id="@+id/tagview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/dialogbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dialog box" />

<Button
    android:id="@+id/clrvaluesbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear Values" />

<Button
    android:id="@+id/aboutbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="About" />

<Button
    android:id="@+id/exitbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Exit" />

    </LinearLayout>

and here is the dialog xml, it uses specific styles which I will list after aswell:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="#336699">
 <TextView style="@style/InfoFont" android:text="Enter a Tag for your Timer" android:layout_gravity="center_horizontal"/>
    <EditText
    android:id="@+id/tagname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:background="#ffffff"
    android:singleLine="true">
    <requestFocus />
</EditText>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_marginTop="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="30" />
    <NumberPicker
        android:id="@+id/numberPicker2"
        android:layout_marginTop="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="30" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView style="@style/InfoFont" android:text="Minutes" android:gravity="center" android:layout_weight="30"  />
    <TextView style="@style/InfoFont" android:text="Seconds" android:gravity="center" android:layout_weight="30"  />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/cancelbtn"
        style="@android:attr/buttonBarButtonStyle"
        android:layout_width="30sp"
        android:layout_height="wrap_content"
        android:text="Cancel" 
        android:layout_weight="50"
        android:background="#669900"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="15sp"
        android:layout_marginBottom="15sp"/>
    <Button
        android:id="@+id/submitbtn"
        style="@android:attr/buttonBarButtonStyle"
        android:layout_width="30sp"
        android:layout_height="wrap_content"
        android:text="Submit" 
        android:layout_weight="50"
        android:background="#669900"
        android:layout_marginRight="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginTop="15sp"
        android:layout_marginBottom="15sp"/>
</LinearLayout>
    </LinearLayout>

and now the styles for the dialog xml, it really is just a modification for the textview to neaten up the code.

    <style name="InfoFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:textStyle">bold</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">-1</item>
        <item name="android:shadowRadius">3</item>
        <item name= "android:typeface">monospace </item>
        <item name= "android:layout_marginTop">15sp </item>
        <item name= "android:layout_marginBottom">15sp </item>
    </style>

Ok, all the code is laid out, I would appreciate any help and insight as to why I am getting a null pointer exception on this.

regards

cchinchoy

¿Fue útil?

Solución

In showDialog() when attempting to get references to components in the layout, call findViewById() on the dialog, not on the activity view hierarchy. That is, cusd.findViewById(...) instead of plain findViewById(...).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top