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

问题描述 投票:0回答:1

我是Android开发的新手,我的应用程序中的onClick函数出现了问题。我在一个活动上有五个按钮,所有的按钮都使用完全相同的代码。

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

它们显然是根据不同的java函数进行了调整,它们看起来都是这样的。

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

其他四个函数完美地工作,没有错误。由于某些原因,只有这个函数会导致崩溃。就像我说的,我是新手,所以我不太明白这里到底出了什么问题(或者如何正确调试)。我在openBook2函数上运行断点,结果在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);
        }
    }

我不知道这是什么意思。我也知道我应该使用点击监听器,因为直接在xml中设置onClick是不好的,但我试着去了解点击监听器,我确实不懂。而且,onClick对我来说,在应用中的其他一百多次都能完美地工作,所以我不认为它有什么问题(尤其是我的应用只有按钮和TextViews)。

如果你需要任何额外的信息,请告诉我!

Logcat错误。

       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>
android onclick
1个回答
0
投票

是的,你的问题就是ScrollView的问题。ScrollView不是这样工作的,它必须在上面有一个布局才能使用.所以只要在你的ScrollView上简单的添加一个布局,然后把你的TextView放在这个布局上。

ScrollView > LinearLayout > TextView

(你可以把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>

就像这样:"编辑"。

第二个问题,也是你代码的主要问题,可能是你给TextView设置的字符串太长了,你可以试着减少文本。

© www.soinside.com 2019 - 2024. All rights reserved.