Делаю своё первое приложение в Android studio и ничего в этом не понимаю. Есть два Activity, и один из них запускается по нажатию кнопки в основном. Вот код MainActivity:
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int score = 0;
boolean alive = true;
String name = "Player";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playB = (Button)findViewById(R.id.button);
Button exit = (Button)findViewById((R.id.button3));
playB.setOnClickListener(this);
}
@Override
public void onClick(View view){
Intent i;
i = new Intent(this, ActivityG.class);
startActivity(i);
}
}
А вот код второго Activity:
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class ActivityG extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g);
int partA = 2;
int partB = 2;
int answ = partA*partB;
int wansw1 = answ - 1;
int wansw2 = answ + 1;
TextView textA = (TextView)findViewById(R.id.textView2);//О, господи, это бъявление объектов в джаве... пипец...
TextView textB = (TextView)findViewById(R.id.textView4);
Button ch1 = (Button)findViewById(R.id.button4);
Button ch2 = (Button)findViewById(R.id.button5);
Button ch3 = (Button)findViewById(R.id.button6);
textA.setText(partA);
textB.setText(partB);
ch1.setText(answ);
ch2.setText(wansw1);
ch3.setText(wansw2);
}
}
Ошибка:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 11018
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.ActivityG}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:683)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:336)
at android.widget.TextView.setText(TextView.java:4465)
at com.example.test.ActivityG.onCreate(ActivityG.java:25)
at android.app.Activity.performCreate(Activity.java:6309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2530)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:683)