我有两个按钮和两个textViews,当我点击“add2_A”时,“score1”会更新并增加2。
同样是“add2_B”会增加“score2”
我想对他们两个只使用一种方法..问题是应用程序崩溃了!
这是我的java代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class app2 extends AppCompatActivity implements View.OnClickListener
{
public int number =0;
public int id;
TextView txt1= (TextView) findViewById(R.id.score1);
TextView txt2 = (TextView) findViewById(R.id.score2);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app2);
}
@Override
public void onClick(View v){
id=v.getId();
}
public void increment2(){
number =number +2;
if(id ==R.id.add2_A)
txt1.setText(number);
if(id== R.id.add2_B)
txt2.setText(String.valueOf(number));
}}
修改: 这是用于调用“increment2”方法的XML代码:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/points2"
android:layout_margin="8dp"
android:id="@+id/add2_A"
android:onClick="increment2"/>
修改: 我解决了它现在有效..这是编辑后的代码:
public class app2 extends AppCompatActivity {
public int number =0;
public int id;
public TextView txt1;
public TextView txt2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app2);
txt1= (TextView) findViewById(R.id.score1);
txt2 = (TextView) findViewById(R.id.score2);
}
public void increment2(View v) {
number = number + 2;
id = v.getId();
if (id == R.id.add2_A) {
txt1 = (TextView) findViewById(R.id.score1);
txt1.setText(String.valueOf(number));
}
if (id == R.id.add2_B) {
txt2 = (TextView) findViewById(R.id.score2);
txt2.setText(String.valueOf(number));
}
}
修改: 这是logcat中出现的内容,消息栏中没有其他消息:
Process: com.example.mayouza.myapplication2, PID: 2553
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mayouza.myapplication2/com.example.mayouza.myapplication2.app2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.mayouza.myapplication2.app2.<init>(app2.java:11)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
在致电findViewById()
之前,您将无法成功致电setContentView()
,因为尚未构建视图层次结构。
您需要在致电findViewById()
后将来电转至setContentView()
。
setContentView(R.layout.activity_app2);
txt1= (TextView) findViewById(R.id.score1);
txt2 = (TextView) findViewById(R.id.score2);