尝试在按下时简单地设置按钮颜色和文本。总是崩溃?

时间:2014-09-10 16:42:22

标签: android button colors settext

所以这是我的代码,我只是试图在按下时Button posNeg_btn的文本和颜色发生变化。但它一直崩溃并给我空指针异常?有任何想法吗?我能错过什么?

ListView recurrenceListView;
Button add_btn;
EditText reccurenceEntry_et;
EditText reccurenceExplanation_et;
Button clear_btn;
Button posNeg_btn;
Boolean positiveIfTrue = false;
//Double[] comparisonArray = new Double[8];
int reccurenceCounter;

String[] reccurenceArray = new String[30];
String[] reccurenceExpArray = new String[30];
String[] finalDisplayArray = new String[30];



private ArrayAdapter arrayAdapter;


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


        recurrenceListView = (ListView) findViewById( R.id.recurrenceListView );
        reccurenceEntry_et = (EditText) findViewById( R.id.reccurenceEntry_et );
        reccurenceExplanation_et = (EditText) findViewById( R.id.reccurenceExplanation_et );

        Button add_btn = (Button) findViewById( R.id.add_btn);
        Button clear_btn = (Button) findViewById( R.id.clear_btn);
        Button posNeg_btn = (Button) findViewById( R.id.posNeg_btn);




        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                addRecurrence();
                //calculateMovingAverage();

            }
        });

        posNeg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                changeSign();
                //calculateMovingAverage();

            }
        });

        clear_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                clearReccurences();
                //calculateMovingAverage();

            }
        });


}

    public void changeSign(){


        posNeg_btn.setText("Test");
        posNeg_btn.setTextColor(Color.GREEN);

        /*
        if(positiveIfTrue ==false){
        positiveIfTrue = true;
        posNeg_btn.setText("+");
      //  posNeg_btn.setBackgroundColor(Color.GREEN);
        }

        if(positiveIfTrue == true){
            positiveIfTrue = false;
            posNeg_btn.setText("-");
        //    posNeg_btn.setBackgroundColor(Color.RED);

        }   
    }

forecast.xml

<?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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Specific Bills/Recurrences" />

<EditText
    android:id="@+id/reccurenceEntry_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Specific Bills/Recurrences"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/reccurenceExplanation_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Explanation" />

<ListView
    android:id="@+id/recurrenceListView"
    android:layout_width="114dp"
    android:layout_height="119dp" >
</ListView>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="191dp"
    android:layout_height="48dp" />

<Button
    android:id="@+id/add_btn"
    android:layout_width="77dp"
    android:layout_height="wrap_content"
    android:text="Add" />

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

<Button
    android:id="@+id/posNeg_btn"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:text="-"
    android:textSize="40sp" />

logcat的

09-10 09:46:06.935: E/AndroidRuntime(12964): FATAL EXCEPTION: main
09-10 09:46:06.935: E/AndroidRuntime(12964): Process: com.example.forecastspending, PID: 12964
09-10 09:46:06.935: E/AndroidRuntime(12964): java.lang.NullPointerException
09-10 09:46:06.935: E/AndroidRuntime(12964):    at       com.example.forecastspending.SavingsForecastActivity.changeSign(SavingsForecastActivity.java:102)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.example.forecastspending.SavingsForecastActivity$2.onClick(SavingsForecastActivity.java:79)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.view.View.performClick(View.java:4442)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.view.View$PerformClick.run(View.java:18473)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Handler.handleCallback(Handler.java:733)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Looper.loop(Looper.java:136)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.app.ActivityThread.main(ActivityThread.java:5105)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at java.lang.reflect.Method.invoke(Method.java:515)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

在你的代码上:

 Button posNeg_btn = (Button) findViewById( R.id.posNeg_btn);

删除按钮。必须是这样的:

posNeg_btn = (Button) findViewById( R.id.posNeg_btn);

其他按钮相同

问题是你正在创建一个局部变量。这样您就可以将侦听器设置为该变量。当你进入函数时,你正在使用该字段,该字段为空,因为你只是设置局部变量而不是字段。