简体   繁体   中英

android:onClick intent crashing app, throws IllegalStateException

I am very new to Android development and I am having an issue with an onClick function in my app. I have five buttons on one activity, and all of them are using the exact same code:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 2"
        android:layout_weight="1"
        android:onClick="openBook2"/>

They're obviously adjusted according to the different java functions, which all look like this:

public void openBook2(View view) {
    Intent i = new Intent(this, Real2.class);
    startActivity(i);
}

The other four functions work perfectly, without error. For some reason, only this function causes a crash. Like I said, I'm new to this so I don't really understand what is going wrong here (or how to properly debug). I ran breakpoints on the openBook2 function and it came back with this in AppCompatViewInflater.java:

@Override
    public void onClick(@NonNull View v) {
        if (mResolvedMethod == null) {
            resolveMethod(mHostView.getContext(), mMethodName);
        }

        try {
            mResolvedMethod.invoke(mResolvedContext, v);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(
                    "Could not execute non-public method for android:onClick", e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException(
                    "Could not execute method for android:onClick", e);
        }
    }

I don't know what this means. I also know that I should be using a click listener because having the onClick directly in the xml is bad but I have tried to understand click listeners and I truly do not. And, the onClick has been working perfectly fine for me about a hundred other times in the app so I don't see a problem with it (especially since my app is only buttons and TextViews).

If you need any additional information, please let me know!

Logcat error:

       2020-04-24 23:37:07.198 19688-19688/co.hyperstudios.elderscrollslibrary E/AndroidRuntime: FATAL EXCEPTION: main
    Process: co.hyperstudios.elderscrollslibrary, PID: 19688
    java.lang.RuntimeException: Unable to start activity ComponentInfo{co.hyperstudios.elderscrollslibrary/co.hyperstudios.elderscrollslibrary.Real2}: android.view.InflateException: Binary XML file line #75 in co.hyperstudios.elderscrollslibrary:layout/activity_real2: Binary XML file line #75 in co.hyperstudios.elderscrollslibrary:layout/activity_real2: Error inflating class TextView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #75 in co.hyperstudios.elderscrollslibrary:layout/activity_real2: Binary XML file line #75 in co.hyperstudios.elderscrollslibrary:layout/activity_real2: Error inflating class TextView
     Caused by: android.view.InflateException: Binary XML file line #75 in co.hyperstudios.elderscrollslibrary:layout/activity_real2: Error inflating class TextView
     Caused by: java.lang.IndexOutOfBoundsException: setSpan (5851 ... 5857) ends beyond length 16
        at android.text.SpannableStringInternal.checkRange(SpannableStringInternal.java:490)
        at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:196)
        at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:183)
        at android.text.SpannableString.setSpan(SpannableString.java:60)
        at android.content.res.StringBlock.applyStyles(StringBlock.java:217)
        at android.content.res.StringBlock.get(StringBlock.java:161)
        at android.content.res.ApkAssets.getStringFromPool(ApkAssets.java:140)
        at android.content.res.AssetManager.getPooledStringForCookie(AssetManager.java:787)
        at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1375)
        at android.content.res.TypedArray.getText(TypedArray.java:233)
        at android.widget.TextView.<init>(TextView.java:1214)
        at android.widget.TextView.<init>(TextView.java:968)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:99)
        at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95)
        at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:182)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1061)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
2020-04-24 23:37:07.198 19688-19688/co.hyperstudios.elderscrollslibrary E/AndroidRuntime:     at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at co.hyperstudios.elderscrollslibrary.Real2.onCreate(Real2.java:14)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

activity_real2 TextView:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Real2"
android:orientation="vertical">

<!--Title and author-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_marginTop="16dp"
    android:textColor="@android:color/black"
    android:text="The Real Barenziah"
    android:layout_gravity="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Plitinius Mero" />

<!--Buttons for different parts of the book-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="8dp"
    android:paddingTop="8dp" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 1"
        android:layout_weight="1"
        android:onClick="openBook1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 2"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 3"
        android:layout_weight="1"
        android:onClick="openBook3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 4"
        android:layout_weight="1"
        android:onClick="openBook4"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book 5"
        android:layout_weight="1"
        android:onClick="openBook5"/>

</LinearLayout>

<!--Book text-->
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/real2"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:paddingRight="16dp"
        android:layout_gravity=""
        android:textSize="15sp" />
    </ScrollView>
</LinearLayout>

Yes, your problem is exactly the ScrollView. ScrollView doesn't work like that it has to have a layout on it to be used. so just add simply a layout on your ScrollView and put your TextView on that layout.

ScrollView > LinearLayout > TextView

(You can change LinearLayout to any ViewGroup type)just like this:

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation:"vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/real2"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                android:paddingRight="16dp"
                android:layout_gravity=""
                android:textSize="15sp" />
    </LinearLayout>
</ScrollView>

Edit:

The Second problem and main problem of your code is probably the long String you had set for TextView just try to reduce the text.

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