简体   繁体   中英

I'm getting an error while running my app

When i try to run my app i get this error:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.mysolver/com.example.mysolver.MainActivity}:
java.lang.ClassCastException:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.Button
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
         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.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
 android.widget.Button
         at com.example.mysolver.MainActivity.onCreate(MainActivity.java:46)
         at android.app.Activity.performCreate(Activity.java:6679)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
         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)

Other people have had this problem but none of the solutions worked for me. Here is my "content_main.xml"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="72dp"
        android:onClick="onClick"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Enter letters"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/output"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Output will appear here..."
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Here's my "activity_main.xml":

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include
        android:id="@+id/button"
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Here's my "MainActivity.java":

package com.example.mysolver;

import android.content.res.AssetManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class MainActivity extends AppCompatActivity {
    Button butt;
    EditText text;
    String letters;
    TextView mOutput;
    processData procDat;

    public static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        Log.d(TAG, "Set up tool bar");

        setSupportActionBar(toolbar);

        text = findViewById(R.id.editText);
        Log.d(TAG, "Set up text");

        mOutput = findViewById(R.id.output);

        butt = findViewById(R.id.button);
        Log.d(TAG, "Set up button");

        AssetManager assetManager = getAssets();
        InputStream inputStream;
        try {
            inputStream = assetManager.open("dictionary.txt");
        } catch (IOException e) {
            inputStream = null;
        }
        Reader reader = new InputStreamReader(inputStream);
        BufferedReader is = new BufferedReader(reader);
        procDat = new processData("", is);
        Log.d(TAG, "Initialized procDat");
    }

    public void onClick(View v)
    {
        Log.d(TAG, "Button clicked");
        String output;
        letters = text.getText().toString();
        procDat.setLetters(letters);

        try {
            output = procDat.doProcessing();
        } catch (IOException e) {
            output = "Error while opening file";
        }
        mOutput.setText(output);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Simply rename the id with other id instead of button. You can't use the same id in two layout since content_main.xml is included in activity_main.xml .

<include
        android:id="@+id/button"
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

the main problem is in your "AndroidManidfest.xml" file and it is corrected by changing the theme of your application or theme of your activity. here are 3 solutions for that...

  1. Changing the theme of all activity by "android:theme="@style/Theme.AppCompat"" or by "android:theme="@style/Theme.MaterialComponents.Light.NoActionBar""
  2. Changing the theme of your application by "android:theme="@style/Theme.AppCompat"" or by "android:theme="@style/Theme.MaterialComponents.Light.NoActionBar""
  3. Or by changing your "androidx.constraintlayout.widget.ConstraintLayout" by "RelativeLayout" in your "content_main.xml" file

I'm sure that it works...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM