我正在尝试做一个地理测验应用程序,但我一直遇到这个问题。
这是我从 logcat 得到的错误。第一页打开,但当我点击开始时,它关闭了应用程序。
致命例外:main
进程:com.example.hangilkebu,PID:29267
java.lang.IllegalStateException: 无法执行方法 机器人:点击
在 androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:473) 在 android.view.View.performClick(View.java:8160)
在 android.view.View.performClickInternal(View.java:8137)
在 android.view.View.access$3700(View.java:888)
在 android.view.View$PerformClick.run(View.java:30236)
在 android.os.Handler.handleCallback(Handler.java:938)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:246)
在 android.app.ActivityThread.main(ActivityThread.java:8595)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
引起:java.lang.reflect.InvocationTargetException
在 java.lang.reflect.Method.invoke(本机方法)
在 androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468) 在 android.view.View.performClick(View.java:8160)
在 android.view.View.performClickInternal(View.java:8137)
在 android.view.View.access$3700(View.java:888)
在 android.view.View$PerformClick.run(View.java:30236)
在 android.os.Handler.handleCallback(Handler.java:938)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:246)
在 android.app.ActivityThread.main(ActivityThread.java:8595)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
原因:android.content.ActivityNotFoundException:无法找到 显式活动类 {com.example.hangilkebu/com.example.hangilkebu.OyunBasladi};你 在您的 AndroidManifest.xml 中声明此活动?
在 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2078) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1727) 在 android.app.Activity.startActivityForResult(Activity.java:5377)
在 androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728) 在 android.app.Activity.startActivityForResult(Activity.java:5335)
在 androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709) 在 android.app.Activity.startActivity(Activity.java:5721)
在 android.app.Activity.startActivity(Activity.java:5674)
在 com.example.hangilkebu.MainActivity.startGame(MainActivity.java:21)
在 java.lang.reflect.Method.invoke(本机方法)
在 androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468) 在 android.view.View.performClick(View.java:8160)
在 android.view.View.performClickInternal(View.java:8137)
在 android.view.View.access$3700(View.java:888)
在 android.view.View$PerformClick.run(View.java:30236)
在 android.os.Handler.handleCallback(Handler.java:938)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:246)
在 android.app.ActivityThread.main(ActivityThread.java:8595)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
此页面在您启动应用程序时启动。我认为错误可能在这里?
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startGame (View view){
Intent intent = new Intent(MainActivity.this, OyunBasladi.class);
startActivity(intent);
finish();
}
}
这是它崩溃的部分。我在第一页点击播放,它应该会把我带到这里。也许这部分有缺陷。
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class OyunBasladi extends AppCompatActivity {
TextView tvTimer;
TextView tvResult;
ImageView ivShowImage;
HashMap<String, Integer> map = new HashMap<>();
ArrayList<String> techList = new ArrayList<>();
int index;
Button btn1, btn2, btn3, btn4;
TextView tvPoints;
int points;
CountDownTimer countDownTimer;
long millisUntilFinished;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soru1);
tvTimer = findViewById(R.id.tvTimer);
tvResult = findViewById(R.id.tvResult);
ivShowImage = findViewById(R.id.ivShowImage);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
tvPoints = findViewById(R.id.tvPoints);
index = 0;
techList.add("Çin");
techList.add("Türkiye");
techList.add("Rusya");
techList.add("D.Kongo Cumhuriyeti");
techList.add("Mısır");
techList.add("Kazakistan");
techList.add("St. Lucia");
techList.add("Trinidad ve Tobago");
techList.add("İsviçre");
techList.add("Lüksemburg");
techList.add("Tonga");
techList.add("Filipinler");
techList.add("Guyana");
techList.add("Guatemala");
techList.add("ABD");
techList.add("Kanada");
techList.add("Peru");
techList.add("Burundi");
map.put(techList.get(0), R.drawable.cin);
map.put(techList.get(1), R.drawable.turkiye);
map.put(techList.get(2), R.drawable.rusya);
map.put(techList.get(3), R.drawable.dkongocumhuriyeti);
map.put(techList.get(4), R.drawable.misir);
map.put(techList.get(5), R.drawable.kazakistan);
map.put(techList.get(6), R.drawable.stlucia);
map.put(techList.get(7), R.drawable.trinidadvetobago);
map.put(techList.get(8), R.drawable.isvicre);
map.put(techList.get(9), R.drawable.luksemburg);
map.put(techList.get(10), R.drawable.tonga);
map.put(techList.get(11), R.drawable.filipinler);
map.put(techList.get(12), R.drawable.guyana);
map.put(techList.get(13), R.drawable.guatemala);
map.put(techList.get(14), R.drawable.abd);
map.put(techList.get(15), R.drawable.kanada);
map.put(techList.get(16), R.drawable.peru);
map.put(techList.get(17), R.drawable.burundi);
Collections.shuffle(techList);
millisUntilFinished = 10000;
points = 0;
startGame();
}
private void startGame() {
millisUntilFinished = 10000;
tvTimer.setText("" + (millisUntilFinished / 1000) + "s");
tvPoints.setText(points + " / " + techList.size());
generateQuestions(index);
countDownTimer = new CountDownTimer(millisUntilFinished, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvTimer.setText("" + (millisUntilFinished / 1000) + "s");
}
@Override
public void onFinish() {
index++;
if (index > techList.size() - 1){
ivShowImage.setVisibility(View.GONE);
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
btn3.setVisibility(View.GONE);
btn4.setVisibility(View.GONE);
Intent intent = new Intent(OyunBasladi.this, OyunBitti.class);
intent.putExtra("points", points);
startActivity(intent);
finish();
}
else {
countDownTimer = null;
startGame();
}
}
}.start();
}
private void generateQuestions(int index) {
ArrayList<String> techListTemp = (ArrayList<String>) techList.clone();
String correctAnswer = techList.get(index);
techListTemp.remove(correctAnswer);
Collections.shuffle(techListTemp);
ArrayList<String> newList = new ArrayList<>();
newList.add(techListTemp.get(0));
newList.add(techListTemp.get(1));
newList.add(techListTemp.get(2));
newList.add(correctAnswer);
Collections.shuffle(newList);
btn1.setText(newList.get(0));
btn2.setText(newList.get(1));
btn3.setText(newList.get(2));
btn4.setText(newList.get(3));
ivShowImage.setImageResource(map.get(techList.get(index)));
}
public void nextQuestion(View view) {
btn1.setBackgroundColor(Color.parseColor("#2196f3"));
btn2.setBackgroundColor(Color.parseColor("#2196f3"));
btn3.setBackgroundColor(Color.parseColor("#2196f3"));
btn4.setBackgroundColor(Color.parseColor("#2196f3"));
btn1.setEnabled(true);
btn2.setEnabled(true);
btn3.setEnabled(true);
btn4.setEnabled(true);
countDownTimer.cancel();
index++;
if (index > techList.size() - 1){
ivShowImage.setVisibility(View.GONE);
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
btn3.setVisibility(View.GONE);
btn4.setVisibility(View.GONE);
Intent intent = new Intent(OyunBasladi.this, OyunBitti.class);
intent.putExtra("points", points);
startActivity(intent);
finish();
}
else
{
countDownTimer = null;
startGame();
}
}
public void answerSelected(View view) {
view.setBackgroundColor(Color.parseColor("#17243e"));
btn1.setEnabled(false);
btn2.setEnabled(false);
btn3.setEnabled(false);
btn4.setEnabled(false);
countDownTimer.cancel();
String answer = ((Button) view).getText().toString().trim();
String correctAnswer = techList.get(index);
if(answer.equals(correctAnswer)){
points++;
tvPoints.setText(points + " / " + techList.size());
tvResult.setText("Doğru");
}
else
{
tvResult.setText("Yanlış");
}
}
}
游戏代码,在您回答所有问题后,它将带您到这里。我认为这个错误的可能性最小,但我还是把它放在这里。
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class OyunBitti extends AppCompatActivity {
TextView tvPoints, tvPersonalBest;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oyun_bitti);
int points = getIntent().getExtras().getInt("points");
tvPoints = findViewById(R.id.tvPoints);
tvPersonalBest = findViewById(R.id.tvPersonalBest);
tvPoints.setText("" + points);
sharedPreferences = getSharedPreferences("pref", 0);
int pointsSP = sharedPreferences.getInt("pointsSP", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
if(points > pointsSP){
pointsSP = points;
editor.putInt("pointsSP", pointsSP);
editor.commit();
}
// Set tvPersonalBest with the value of pointsSP
tvPersonalBest.setText("" + pointsSP);
}
public void restart(View view) {
Intent intent = new Intent(OyunBitti.this, OyunBasladi.class);
startActivity(intent);
finish();
}
public void exit(View view) {
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"
tools:targetApi="31">
<activity
android:name=".OyunBasladi"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e8f4f8"
android:gravity="center"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yenidenoyna"
android:onClick="restart" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cik"
android:onClick="exit" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Puan: "
android:textColor="#0b80a7"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvPoints"
android:layout_weight="1"
android:textColor="@android:color/holo_green_light"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="En iyi puan: "
android:textColor="#0b80a7"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvPersonalBest"
android:layout_weight="1"
android:textColor="#ff6000"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="234dp"
android:layout_height="129dp"
android:layout_marginTop="5dp"
app:srcCompat="@drawable/oyunbitti" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="64dp"
android:layout_weight="1" />
</LinearLayout>
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.hangilkebu/com.example.hangilkebu.OyunBasladi}; have you declared this activity in your AndroidManifest.xml?
您在文件 AndroidManifest 中声明了您的活动?
要使活动正常运行,您必须记得添加它们。
你应该在
OyunBasladi
中添加AndroidManifest.xml
。
像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"
tools:targetApi="31">
<activity
android:name=".OyunBitti"
android:exported="false" />
<activity
android:name=".OyunBasladi"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
解决问题!谢谢 Halil Özel 的回答!