null点异常android java类

时间:2015-02-03 14:43:11

标签: java android nullpointerexception counter

我正在尝试实现一个保存足球事实的java类。它允许用户单击图像按钮以增加值。 a为每个按钮保留不同的int值,当用户单击图像按钮时,计数器会递增,然后在各个editTexts上显示。 活动运行时,我得到一个零点异常。 有人为什么我会在第68行得到这个零点异常?

package com.example.buttondemo;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.EditText;
import android.app.Activity;

public class FootballFacts extends Activity implements OnClickListener {

    ImageButton btnGoalBlue,btnGoalRed,btnFoulBlue,btnFoulRed,btnCornerBlue,btnCornerRed,btnYellowBlue,btnYellowRed,
    btnRedBlue,btnRedRed, btnRestartValues;
   EditText textGoalBlue,textGoalRed,textFoulBlue,textFoulRed,textYellowBlue,textYellowRed,textRedBlue,
   textRedRed,textCornerBlue,textCornerRed;
   int counterBlueGoal,counterRedGoal,counterCornerBlue,counterCornerRed,counterFoulBlue,counterFoulRed,counterYellowBlue,counterYellowRed,
   counterRedBlue,counterRedRed= 0;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_football_facts);
       //find the buttons in layout
       btnGoalBlue = (ImageButton)findViewById(R.id.btnGoalBlue);
       btnGoalRed = (ImageButton)findViewById(R.id.btnGoalRed);

       btnFoulBlue = (ImageButton)findViewById(R.id.btnFoulBlue);       
       btnFoulRed = (ImageButton)findViewById(R.id.btnFoulRed);

       btnCornerBlue = (ImageButton)findViewById(R.id.btnCornerBlue);
       btnCornerRed = (ImageButton)findViewById(R.id.btnCornerRed);

       btnYellowBlue = (ImageButton)findViewById(R.id.btnYellowBlue);
       btnYellowRed = (ImageButton)findViewById(R.id.btnYellowRed);

       btnRedBlue = (ImageButton)findViewById(R.id.btnRedBlue);
       btnRedRed = (ImageButton)findViewById(R.id.btnRedRed);

       btnRestartValues = (ImageButton)findViewById(R.id.btnResetFacts);

       //textfields
       textGoalBlue = (EditText)findViewById(R.id.editTextGoalBlue);
       textGoalRed = (EditText)findViewById(R.id.editTextGoalRed);

       textFoulBlue = (EditText)findViewById(R.id.editTextFoulBlue);
       textFoulRed = (EditText)findViewById(R.id.editTextFoulRed);

       textYellowBlue = (EditText)findViewById(R.id.editTextYellowBlue);
       textYellowRed = (EditText)findViewById(R.id.editTextYellowRed);

       textRedBlue = (EditText)findViewById(R.id.editTextRedBlue);
       textRedRed = (EditText)findViewById(R.id.editTextRedRed);

       textCornerBlue = (EditText)findViewById(R.id.editTextCornerBlue);
       textCornerRed = (EditText)findViewById(R.id.editTextCornerRed);



       //---set on click listeners on the buttons-----
       btnGoalBlue.setOnClickListener(this);
       btnGoalRed.setOnClickListener(this);
       btnCornerBlue.setOnClickListener(this);
       btnCornerRed.setOnClickListener(this);
       btnFoulBlue.setOnClickListener(this);
       btnFoulRed.setOnClickListener(this);
       btnYellowBlue.setOnClickListener(this);
       btnYellowRed.setOnClickListener(this);
       btnRedBlue.setOnClickListener(this);
       btnRedRed.setOnClickListener(this);
       btnRestartValues.setOnClickListener(this);

   }

   @Override
   public void onClick(View v) {
       if (v == btnGoalBlue){
           counterBlueGoal++;
           textGoalBlue.setText(Integer.toString(counterBlueGoal));
       }
       if (v == btnGoalRed){
           counterRedGoal++;
           textGoalRed.setText(Integer.toString(counterRedGoal));
       }
       if (v == btnFoulBlue){
           counterFoulBlue++;
           textFoulBlue.setText(Integer.toString(counterFoulBlue));
       }
       if (v == btnFoulRed){
           counterFoulRed++;
           textFoulRed.setText(Integer.toString(counterFoulRed));
       }
       if (v == btnCornerBlue){
           counterCornerBlue++;
           textCornerBlue.setText(Integer.toString(counterCornerBlue));
       }
       if (v == btnCornerRed){
           counterCornerRed++;
           textCornerRed.setText(Integer.toString(counterCornerRed));
       }
       if (v == btnYellowBlue){
           counterYellowBlue++;
           textYellowBlue.setText(Integer.toString(counterYellowBlue));
       }
       if (v == btnYellowRed){
           counterYellowRed++;
           textYellowRed.setText(Integer.toString(counterYellowRed));
       }
       if (v == btnRedBlue){
           counterRedBlue++;
           textRedBlue.setText(Integer.toString(counterRedBlue));
       }
       if (v == btnRedRed){
           counterRedRed++;
           textRedRed.setText(Integer.toString(counterRedRed));
       }


       if (v == btnRestartValues){
           counterBlueGoal = 0;
           counterRedGoal = 0;
           counterCornerBlue = 0;
           counterCornerRed = 0;
           counterYellowBlue = 0;
           counterYellowRed = 0;
           counterRedBlue = 0;
           counterRedRed = 0;
           counterFoulBlue = 0;
           counterFoulRed = 0;

           textGoalBlue.setText(Integer.toString(counterBlueGoal));
           textGoalRed.setText(Integer.toString(counterRedGoal));

           textYellowBlue.setText(Integer.toString(counterYellowBlue));
           textYellowRed.setText(Integer.toString(counterYellowRed));

           textRedBlue.setText(Integer.toString(counterRedBlue));
           textRedRed.setText(Integer.toString(counterRedRed));

           textCornerBlue.setText(Integer.toString(counterCornerBlue));
           textCornerRed.setText(Integer.toString(counterCornerRed));

           textFoulBlue.setText(Integer.toString(counterFoulBlue));
           textFoulRed.setText(Integer.toString(counterFoulRed));

       }
   }

}

logcat文件

-03 14:39:10.959: D/AndroidRuntime(7371): Shutting down VM
02-03 14:39:10.959: W/dalvikvm(7371): threadid=1: thread exiting with uncaught exception (group=0x415e6ce0)
02-03 14:39:10.969: E/AndroidRuntime(7371): FATAL EXCEPTION: main
02-03 14:39:10.969: E/AndroidRuntime(7371): Process: com.example.buttondemo, PID: 7371
02-03 14:39:10.969: E/AndroidRuntime(7371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.FootballFacts}: java.lang.NullPointerException
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.os.Looper.loop(Looper.java:136)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.main(ActivityThread.java:5146)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at java.lang.reflect.Method.invoke(Method.java:515)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at dalvik.system.NativeStart.main(Native Method)
02-03 14:39:10.969: E/AndroidRuntime(7371): Caused by: java.lang.NullPointerException
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.example.buttondemo.FootballFacts.onCreate(FootballFacts.java:68)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.Activity.performCreate(Activity.java:5231)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
02-03 14:39:10.969: E/AndroidRuntime(7371):     ... 11 more

2 个答案:

答案 0 :(得分:0)

您所要做的就是转到第60行并确保您的所有姓名和身份证都正确无误。如果这不起作用。我还在调查这个,但我相信android studio中可能会有一个小错误,初始化某些视图。如果/当它发生在我身上时,我会检查ID,如果不能重新启动程序,如果这不起作用,则更改有问题的按钮的名称。通常第二个陡峭的问题为我解决了问题。希望这有帮助!

答案 1 :(得分:0)

来自活动:

  /**
 * Finds a view that was identified by the id attribute from the XML that
 * was processed in {@link #onCreate}.
 *
 * @return The view if found or null otherwise.
 */
public View findViewById(int id) {
    return getWindow().findViewById(id);
}

似乎为您返回null。该视图是否位于您在setContentView()中设置的相同布局中?

问题似乎出现在布局XML文件中而不是代码中。这个看起来很好。