简体   繁体   English

android:onClick 意图崩溃应用程序,抛出 IllegalStateException

[英]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.我对 Android 开发非常陌生,我在我的应用程序中遇到了 onClick function 的问题。 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:它们显然是根据不同的 java 功能进行了调整,看起来像这样:

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.由于某种原因,只有这个 function 会导致崩溃。 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:我在 openBook2 function 上运行断点,它在 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.我也知道我应该使用点击监听器,因为将 onClick 直接放在 xml 中是不好的,但我试图了解点击监听器,但我真的没有。 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).而且,onClick 在应用程序中对我来说已经工作了大约一百次,所以我看不到它有问题(特别是因为我的应用程序只有按钮和 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: 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。 ScrollView doesn't work like that it has to have a layout on it to be used. ScrollView 不能像这样工作,它必须有一个布局才能使用。 so just add simply a layout on your ScrollView and put your TextView on that layout.因此,只需在 ScrollView 上添加一个布局,然后将 TextView 放在该布局上。

ScrollView > LinearLayout > TextView

(You can change LinearLayout to any ViewGroup type)just like this: (您可以将 LinearLayout 更改为任何 ViewGroup 类型)就像这样:

<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.您的代码的第二个问题和主要问题可能是您为 TextView 设置的长字符串只是尝试减少文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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