2つのボタンと2つのtextViewがあります。「add2_A」をクリックすると「score1」が更新され、2ずつ増分されます。2つのtextViewsを変更するJavaメソッドを構築
同じことが「add2_B」..the問題があることを、私はそれらの両方のための1つの方法だけを使用したい
を「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));
}}
は編集:
<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"/>
が編集: これは "increment2" メソッドを呼び出すためのXMLコードである を私はそれとそれを解決これは編集後のコードです:
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));
}
}
編集:
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)
エラーメッセージを添付できますか? –
質問を編集しました。 –
エラーはこのアクティビティから来ていませんが、おそらくComponentInfoがありません。 – Vega