How do I solve "Could not execute method for android :onClick

huangapple go评论77阅读模式
英文:

How do I solve "Could not execute method for android :onClick

问题

I have translated the provided content for you. Here are the translations:

  1. "I'm trying to do an geography quiz app but I keep getting this problem."

    • 我正在尝试创建一个地理问答应用,但我一直遇到这个问题。
  2. "This is the error I'm getting from the logcat. The first page opens but the moment I click start it closes the app."

    • 这是我从logcat中得到的错误。第一页打开,但当我点击开始时,应用程序就关闭了。
  3. "Main Activity"

    • 主活动
  4. "This page starts when you start the app. I think the mistake might be here?"

    • 当您启动应用程序时,这个页面会启动。我觉得问题可能出在这里?
  5. "Start Game"

    • 开始游戏
  6. "This is the part where it crashes. I click play in the first page and it's supposed to take me here. Maybe this part has the flaw."

    • 这是应用程序崩溃的部分。我在第一页点击开始游戏,应该带我到这里。也许这部分有问题。
  7. "Finish Game"

    • 游戏结束
  8. "Codes for the game where after you answered all the questions it will take you here. I think this has the least chance of the mistake but Im still putting it here."

    • 这是游戏的代码,在你回答完所有问题后,会带你到这里。我认为这个部分出错的可能性最小,但我还是放在这里。
  9. "Manifest XML"

    • 清单XML
  10. (Manifest XML code, unchanged)

  11. "Main Activity XML"

  • 主活动XML
  1. (Main Activity XML code, unchanged)

If you have any more text to translate or need further assistance, please let me know.

英文:

I'm trying to do an geography quiz app but I keep getting this problem.

This is the error I'm getting from the logcat. The first page opens but the moment I click start it closes the app.

> FATAL EXCEPTION: main
> Process: com.example.hangilkebu, PID: 29267
> java.lang.IllegalStateException: Could not execute method for
> android:onClick
> at
> androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:473)
> at android.view.View.performClick(View.java:8160)
> at android.view.View.performClickInternal(View.java:8137)
> at android.view.View.access$3700(View.java:888)
> at android.view.View$PerformClick.run(View.java:30236)
> at android.os.Handler.handleCallback(Handler.java:938)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:246)
> at android.app.ActivityThread.main(ActivityThread.java:8595)
> at java.lang.reflect.Method.invoke(Native Method)
> at
> com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
> Caused by: java.lang.reflect.InvocationTargetException
> at java.lang.reflect.Method.invoke(Native Method)
> at
> androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468)
> at android.view.View.performClick(View.java:8160) 
> at android.view.View.performClickInternal(View.java:8137) 
> at android.view.View.access$3700(View.java:888) 
> at android.view.View$PerformClick.run(View.java:30236) 
> at android.os.Handler.handleCallback(Handler.java:938) 
> at android.os.Handler.dispatchMessage(Handler.java:99) 
> at android.os.Looper.loop(Looper.java:246) 
> at android.app.ActivityThread.main(ActivityThread.java:8595) 
> at java.lang.reflect.Method.invoke(Native Method) 
> at
> com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
> 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?
> at
> android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2078)
> at
> android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
> at android.app.Activity.startActivityForResult(Activity.java:5377)
> at
> androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728)
> at android.app.Activity.startActivityForResult(Activity.java:5335)
> at
> androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709)
> at android.app.Activity.startActivity(Activity.java:5721)
> at android.app.Activity.startActivity(Activity.java:5674)
> at
> com.example.hangilkebu.MainActivity.startGame(MainActivity.java:21)
> at java.lang.reflect.Method.invoke(Native Method) 
> at
> androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468) 
> at android.view.View.performClick(View.java:8160) 
> at android.view.View.performClickInternal(View.java:8137) 
> at android.view.View.access$3700(View.java:888) 
> at android.view.View$PerformClick.run(View.java:30236) 
> at android.os.Handler.handleCallback(Handler.java:938) 
> at android.os.Handler.dispatchMessage(Handler.java:99) 
> at android.os.Looper.loop(Looper.java:246) 
> at android.app.ActivityThread.main(ActivityThread.java:8595) 
> at java.lang.reflect.Method.invoke(Native Method) 
> at
> com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Main Activity

This page starts when you start the app. I think the mistake mighg be here?

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();
    }
}

Start Game

This is the part where it crashes. I click play in the first page and it's supposed to take me here. Maybe this part has the flaw.

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ış");
        }
    }
}

Finish Game

Codes for the game where after you answered all the questions it will take you here. I think this has the least chance of the mistake but Im still putting it here.

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();
    }
}

Manifest 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=".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>

Main Activity XML

<?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>

答案1

得分: 0

你应该在 AndroidManifest.xml 中添加 OyunBitti,如下所示:

    <?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>
英文:

For activities to work correctly, you must remember to add them.

You should add OyunBitti in AndroidManifest.xml.

Like this:

    <?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>

huangapple
  • 本文由 发表于 2023年5月7日 03:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定