在数字选择器上获取空指针异常 - Android

时间:2014-05-14 07:25:26

标签: android timer numberpicker

我正在尝试做一个简单的数字选择器"自定义的功能(使用NumberPicker小部件),并排对齐其中的3个。我有一个工作正常,但是一旦我添加了其他2,然后尝试更改变量名称(并在MainActivity中调整它),程序崩溃了NullPointer Exception。它说line 29caused by中的罪魁祸首(MainActivity)。

我可能不太了解MainActivity代码,因为它是我借用的代码。为什么会崩溃?感谢。

MainActivity.java

package com.azurespot.numbpicksample;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {
    Button btnUp, btnDown;
    TextView textViewUp, textViewMid, textViewBottom;

    int nStart = 0;
    int nEnd = 59;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnUp = (Button) findViewById(R.id.buttonUp1|R.id.buttonUp2|R.id.buttonUp3);
        btnDown = (Button) findViewById(R.id.buttonDown1|R.id.buttonDown2|R.id.buttonDown3);

        // All the numbers
        textViewUp = (TextView) findViewById(R.id.plus1|R.id.plus2|R.id.plus3);
        textViewMid = (TextView) findViewById(R.id.mainNumber1|R.id.mainNumber2|R.id.mainNumber3);
        textViewBottom = (TextView) findViewById(R.id.minus1|R.id.minus2|R.id.minus3);

        textViewUp.setText("1"); // Line 29
        textViewMid.setText("0");
        textViewBottom.setText("59");

        btnUp.setOnClickListener(this);
        btnDown.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        String getString = String.valueOf(textViewMid.getText());
        int current = Integer.parseInt(getString);
        if (v == btnUp) {
            if (current < nEnd) {
                current++;
                textViewUp.setText(String.valueOf(current - 1));
                textViewMid.setText(String.valueOf(current));
                textViewBottom.setText(String.valueOf(current + 1));
            }

        }
        if (v == btnDown) {
            if (current > nStart) {
                current--;
                textViewUp.setText(String.valueOf(current - 1));
                textViewMid.setText(String.valueOf(current));
                textViewBottom.setText(String.valueOf(current + 1));
            }
        }
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="vertical" >

        <Button
            android:id="@+id/buttonUp1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:background="@drawable/up_btn" />

        <TextView
            android:id="@+id/plus1"
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="plus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <View
            android:id="@+id/topBar1"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <TextView
            android:id="@+id/mainNumber1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="Main"
            android:textColor="#000000"
            android:textSize="30sp" />

        <View
            android:id="@+id/lowerBar1"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_marginBottom="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#000000" />

        <TextView
            android:id="@+id/minus1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="minus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonDown1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/down_btn" />

    </LinearLayout>

<!-- start center numberpicker -->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/buttonUp2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:background="@drawable/up_btn" />

        <TextView
            android:id="@+id/plus2"
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="plus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <View
            android:id="@+id/topBar2"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <TextView
            android:id="@+id/mainNumber2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="Main"
            android:textColor="#000000"
            android:textSize="30sp" />

        <View
            android:id="@+id/lowerBar2"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_marginBottom="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#000000" />

        <TextView
            android:id="@+id/minus2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="minus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonDown2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/down_btn" />

    </LinearLayout>

    <!-- start right numberpicker -->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/buttonUp3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:background="@drawable/up_btn" />

        <TextView
            android:id="@+id/plus3"
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="plus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <View
            android:id="@+id/topBar3"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:background="#000000" />

        <TextView
            android:id="@+id/mainNumber3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="Main"
            android:textColor="#000000"
            android:textSize="30sp" />

        <View
            android:id="@+id/lowerBar3"
            android:layout_width="30dp"
            android:layout_height="1dp"
            android:layout_marginBottom="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#000000" />

        <TextView
            android:id="@+id/minus3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:text="minus"
            android:textColor="#cc8400"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonDown3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/down_btn" />

    </LinearLayout>

</LinearLayout>

logcat的

FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.azurespot.numbpicksample/com.azurespot.numbpicksample.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.azurespot.numbpicksample.MainActivity.onCreate(MainActivity.java:29)
            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)

3 个答案:

答案 0 :(得分:1)

您可以在R.java文件中找到每个视图的ID。这是自动生成(无法更改)和唯一。语法允许您使用OR。因为结果将是一个int。但是可能没有结果的id的视图R.id.buttonUp1 | R.id.buttonUp2 | R.id.buttonUp3。这就是NPE的原因

答案 1 :(得分:1)

初始化时,您无法使用|分隔布局变量..

所以替换这个

btnUp = (Button) findViewById(R.id.buttonUp1|R.id.buttonUp2|R.id.buttonUp3); 

作为

 btnUp = (Button) findViewById(R.id.buttonUp1);

同样也为别人做了..

答案 2 :(得分:0)

// try this way,hope this will help you...

public class MainActivity extends Activity implements View.OnClickListener {
    Button buttonUp1;
    Button buttonUp2;
    Button buttonUp3;
    Button buttonDown1;
    Button buttonDown2;
    Button buttonDown3;
    TextView mainNumber1;
    TextView mainNumber2;
    TextView mainNumber3;
    TextView plus1;
    TextView plus2;
    TextView plus3;
    TextView minus1;
    TextView minus2;
    TextView minus3;


    int nStart = 0;
    int nEnd = 59;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonUp1 = (Button) findViewById(R.id.buttonUp1);
        buttonUp2 = (Button) findViewById(R.id.buttonUp2);
        buttonUp3 = (Button) findViewById(R.id.buttonUp3);
        buttonDown1 = (Button) findViewById(R.id.buttonDown1);
        buttonDown2 = (Button) findViewById(R.id.buttonDown2);
        buttonDown3 = (Button) findViewById(R.id.buttonDown3);


        // All the numbers
        mainNumber1 = (TextView) findViewById(R.id.mainNumber1);
        mainNumber2 = (TextView) findViewById(R.id.mainNumber2);
        mainNumber3 = (TextView) findViewById(R.id.mainNumber3);
        plus1 = (TextView) findViewById(R.id.plus1);
        plus2 = (TextView) findViewById(R.id.plus2);
        plus3 = (TextView) findViewById(R.id.plus3);
        minus1 = (TextView) findViewById(R.id.minus1);
        minus2 = (TextView) findViewById(R.id.minus2);
        minus3 = (TextView) findViewById(R.id.minus3);


        plus1.setText("1");
        plus2.setText("1");
        plus3.setText("1");
        minus1.setText("59");
        minus2.setText("59");
        minus3.setText("59");
        mainNumber1.setText("0");
        mainNumber2.setText("0");
        mainNumber3.setText("0");

        buttonUp1.setOnClickListener(this);
        buttonUp2.setOnClickListener(this);
        buttonUp3.setOnClickListener(this);
        buttonDown1.setOnClickListener(this);
        buttonDown2.setOnClickListener(this);
        buttonDown3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonUp1 :
                int upp1 = Integer.parseInt(plus1.getText().toString());
                int upm1 = Integer.parseInt(mainNumber1.getText().toString());
                int upd1 = Integer.parseInt(minus1.getText().toString());
                if (upm1 < nEnd) {
                    upm1++;
                    plus1.setText(String.valueOf(upp1 + 1));
                    mainNumber1.setText(String.valueOf(upm1));
                    minus1.setText(String.valueOf(upd1 - 1));
                }
            break;
            case R.id.buttonUp2 :
                int upp2 = Integer.parseInt(plus2.getText().toString());
                int upm2 = Integer.parseInt(mainNumber2.getText().toString());
                int upd2 = Integer.parseInt(minus2.getText().toString());
                if (upm2 < nEnd) {
                    upm2++;
                    plus2.setText(String.valueOf(upp2 + 1));
                    mainNumber2.setText(String.valueOf(upm2));
                    minus2.setText(String.valueOf(upd2 - 1));
                }
                break;
            case R.id.buttonUp3 :
                int upp3 = Integer.parseInt(plus3.getText().toString());
                int upm3 = Integer.parseInt(mainNumber3.getText().toString());
                int upd3 = Integer.parseInt(minus3.getText().toString());
                if (upm3 < nEnd) {
                    upm3++;
                    plus3.setText(String.valueOf(upp3 + 1));
                    mainNumber3.setText(String.valueOf(upm3));
                    minus3.setText(String.valueOf(upd3 - 1));
                }
                break;
            case R.id.buttonDown1 :
                int downp1 = Integer.parseInt(plus1.getText().toString());
                int downm1 = Integer.parseInt(mainNumber1.getText().toString());
                int downd1 = Integer.parseInt(minus1.getText().toString());
                if (downm1 > nStart) {
                    downm1--;
                    plus1.setText(String.valueOf(downp1 - 1));
                    mainNumber1.setText(String.valueOf(downm1));
                    minus1.setText(String.valueOf(downd1 + 1));
                }
                break;
            case R.id.buttonDown2 :
                int downp2 = Integer.parseInt(plus2.getText().toString());
                int downm2 = Integer.parseInt(mainNumber2.getText().toString());
                int downd2 = Integer.parseInt(minus2.getText().toString());
                if (downm2 > nStart) {
                    downm2--;
                    plus2.setText(String.valueOf(downp2 - 1));
                    mainNumber2.setText(String.valueOf(downm2));
                    minus2.setText(String.valueOf(downd2 + 1));
                }
                break;
            case R.id.buttonDown3 :
                int downp3 = Integer.parseInt(plus3.getText().toString());
                int downm3 = Integer.parseInt(mainNumber3.getText().toString());
                int downd3 = Integer.parseInt(minus3.getText().toString());
                if (downm3 > nStart) {
                    downm3--;
                    plus3.setText(String.valueOf(downp3 - 1));
                    mainNumber3.setText(String.valueOf(downm3));
                    minus3.setText(String.valueOf(downd3 + 1));
                }
                break;
        }
    }
}