当我点击按钮

时间:2018-01-31 11:45:33

标签: android

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.amarjeet.goal";
    TextView t1,t2;

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

    }
    public void goal1(View view){
        t1=findViewById(R.id.t1);
      t1.setText(""+(Integer.parseInt(t1.getText().toString())+1));
    }
    public  void goal2(View view){
        t2=findViewById(R.id.t2);
        t2.setText(""+(Integer.parseInt(t2.getText().toString())+1));
    }
    public void winner(View view){
        Intent intent =new Intent(this,Winner.class);
        String msg;
        if(Integer.parseInt(t2.getText().toString()) > Integer.parseInt(t1.getText().toString())){
                            msg="Winner is Team B";}
        else if(Integer.parseInt(t1.getText().toString()) > Integer.parseInt(t2.getText().toString())) {
            msg = "Winner is Team A";
        }
        else
            {
            msg="Match is Draw";}
        intent.putExtra(EXTRA_MESSAGE, msg);
        startActivity(intent);
    }
}
  

错误显示是:::::: E / AndroidRuntime:FATAL EXCEPTION:main                         处理:com.example.amarjeet.goal,PID:24396                         java.lang.IllegalStateException:无法执行android:onClick的方法                             在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)                             在android.view.View.performClick(View.java:6267)                             在android.view.View $ PerformClick.run(View.java:24763)                             在android.os.Handler.handleCallback(Handler.java:789)                             在android.os.Handler.dispatchMessage(Handler.java:98)                             在android.os.Looper.loop(Looper.java:164)                             在android.app.ActivityThread.main(ActivityThread.java:6548)                             at java.lang.reflect.Method.invoke(Native Method)                             在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                          引起:java.lang.reflect.InvocationTargetException                             at java.lang.reflect.Method.invoke(Native Method)                             在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)                             在android.view.View.performClick(View.java:6267)                             在android.view.View $ PerformClick.run(View.java:24763)                             在android.os.Handler.handleCallback(Handler.java:789)                             在android.os.Handler.dispatchMessage(Handler.java:98)                             在android.os.Looper.loop(Looper.java:164)                             在android.app.ActivityThread.main(ActivityThread.java:6548)                             at java.lang.reflect.Method.invoke(Native Method)                             在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                          引起:java.lang.NullPointerException:尝试调用虚方法'java.lang.CharSequence       在null对象引用上的android.widget.TextView.getText()'                             在com.example.amarjeet.goal.MainActivity.winner(MainActivity.java:31)                             at java.lang.reflect.Method.invoke(Native Method)                             在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)                             在android.view.View.performClick(View.java:6267)                             在android.view.View $ PerformClick.run(View.java:24763)                             在android.os.Handler.handleCallback(Handler.java:789)                             在android.os.Handler.dispatchMessage(Handler.java:98)                             在android.os.Looper.loop(Looper.java:164)                             在android.app.ActivityThread.main(ActivityThread.java:6548)                             at java.lang.reflect.Method.invoke(Native Method)                             在``com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)       if语句中的消息显示错误我单击我关闭的按钮

3 个答案:

答案 0 :(得分:0)

当textview为null时,会对t1.getText()t2.getText()进行一次调用。检查他们是否引用了正确的资源,并且在您尝试呼叫getText()时实际创建了它们。

答案 1 :(得分:0)

正如Android会指出的那样,调用 var questions = new List<Question>() { new Question { Text = "Q. [Attack On Titan] Which character is the 'Rogue' Titan?", Answer = "Eren"}, new Question { Text = "Q. [Pokemon] Which Pokemon does Ash use mostly?", Answer = "Pikachu" }, new Question { Text = "Q. [Fairy Tail] Who raised Natsu Dragneel when he was a child?", Answer = "Igneel" }, new Question { Text = "Q. [Death Note] What was Light's surname?", Answer = "Yagami" }, new Question { Text = "Q. [Attack On Titan] Who was Eren's best friend?", Answer = "Armin" }, new Question { Text = "Q. [Attack On Titan] Which character is the'Armored' Titan?", Answer = "Reiner" }, new Question { Text = "Q. [Attack On Titan] Which character is the 'Colossal' Titan?", Answer = "Bertholt" }, new Question { Text = "Q. [Death Note] In the series, there was always a shinigami with Light Yagami, what was their name?", Answer = "Ryuk" }, new Question { Text = "Q. [Attack On Titan] Who gave Mikasa their red scarf?", Answer = "Eren" }, }; Random random = new Random(); int correctAnswers = 0; foreach (var question in questions.OrderBy(q => random.Next())) { Console.WriteLine(question.Text); string answer = ""; do { answer = Console.ReadLine(); if (question.IsCorrect(answer)) { Console.WriteLine($"That is correct! {++correctAnswers}/100"); Thread.Sleep(800); Console.Clear(); } Console.WriteLine("You are incorrect."); System.Threading.Thread.Sleep(40); } 时,t1t2null。但是,可以通过在winner()方法中保留对TextView s的引用来解决此问题,如下所示:

onCreate()

这可确保您在活动的整个生命周期内始终引用@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1 = findViewById(R.id.t1); t2 = findViewById(R.id.t2); } t1,假设这些ID当然有效。另外,请务必从t2goal1()方法中删除这些作业,例如

goal2()

答案 2 :(得分:0)

在使用视图的变量或对象之前,需要绑定视图。您在单独的方法中绑定t1t2,如果在绑定t1t2之前单击获胜按钮,则会崩溃。以下代码可能对您有所帮助。

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.amarjeet.goal";
    TextView t1,t2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=findViewById(R.id.t1);
        t2=findViewById(R.id.t2);
    }
    public void goal1(View view){

      t1.setText(""+(Integer.parseInt(t1.getText().toString())+1));
    }
    public  void goal2(View view){

        t2.setText(""+(Integer.parseInt(t2.getText().toString())+1));
    }
    public void winner(View view){
        Intent intent =new Intent(this,Winner.class);
        String msg;
        if(Integer.parseInt(t2.getText().toString()) > Integer.parseInt(t1.getText().toString())){
                            msg="Winner is Team B";}
        else if(Integer.parseInt(t1.getText().toString()) > Integer.parseInt(t2.getText().toString())) {
            msg = "Winner is Team A";
        }
        else
            {
            msg="Match is Draw";}
        intent.putExtra(EXTRA_MESSAGE, msg);
        startActivity(intent);
    }
}