使用片段启动应用程序时获取“运行时异常”

克里希纳

我是Android的新手,也是Fragments的新手我正在尝试使用一些在线网站指南来实现一个示例Fragments应用程序。

当我实现示例Fragment应用程序时,我在模拟器中启动应用程序时遇到了RunTimeException

Comeplete异常堆栈如下所示:

    04-01 03:24:19.193: E/AndroidRuntime(1374): FATAL EXCEPTION: main
04-01 03:24:19.193: E/AndroidRuntime(1374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentex/com.example.fragmentex.MainActivity}: android.view.InflateException: Binary XML file line #27: Error inflating class fragment
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.os.Looper.loop(Looper.java:137)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at java.lang.reflect.Method.invoke(Method.java:511)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at dalvik.system.NativeStart.main(Native Method)
04-01 03:24:19.193: E/AndroidRuntime(1374): Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class fragment
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Activity.setContentView(Activity.java:1881)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at com.example.fragmentex.MainActivity.onCreate(MainActivity.java:16)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Activity.performCreate(Activity.java:5104)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-01 03:24:19.193: E/AndroidRuntime(1374):     ... 11 more
04-01 03:24:19.193: E/AndroidRuntime(1374): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.javacodegeeks.android.fragmentstest.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Fragment.instantiate(Fragment.java:592)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Fragment.instantiate(Fragment.java:560)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Activity.onCreateView(Activity.java:4709)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-01 03:24:19.193: E/AndroidRuntime(1374):     ... 21 more
04-01 03:24:19.193: E/AndroidRuntime(1374): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.javacodegeeks.android.fragmentstest.FragmentOne" on path: /data/app/com.example.fragmentex-2.apk
04-01 03:24:19.193: E/AndroidRuntime(1374):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-01 03:24:19.193: E/AndroidRuntime(1374):     at android.app.Fragment.instantiate(Fragment.java:582)
04-01 03:24:19.193: E/AndroidRuntime(1374):     ... 24 more

我的Android代码是:

MainActivity.Java

package com.example.fragmentex;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
        Fragment fr;

        if (view == findViewById(R.id.bFrag2)) {
            fr = new FragmentTwo();

        } else {
            fr = new FragmentOne();
        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fr);
        fragmentTransaction.commit();

    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bFrag1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment 1" />

    <Button
        android:id="@+id/bFrag2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment 2" />

    <fragment
        android:id="@+id/fragment1"
        android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.84" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fragmentex"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.fragmentex.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

FragmentOne.java

package com.example.fragmentex;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment{

    public View onCreateView(LayoutInflater inflater,
                  ViewGroup container, Bundle savedInstanceState) {

                   //Inflate the layout for this fragment

              return inflater.inflate(
                      R.layout.fragment_one, container, false);
           }  

}

新日志

04-02 10:13:07.143: E/AndroidRuntime(849): FATAL EXCEPTION: main
04-02 10:13:07.143: E/AndroidRuntime(849): android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
04-02 10:13:07.143: E/AndroidRuntime(849):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-02 10:13:07.143: E/AndroidRuntime(849):  at com.example.fragmentex.FragmentTwo.onCreateView(FragmentTwo.java:16)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.Fragment.performCreateView(Fragment.java:1695)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.os.Handler.handleCallback(Handler.java:725)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.os.Looper.loop(Looper.java:137)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-02 10:13:07.143: E/AndroidRuntime(849):  at java.lang.reflect.Method.invokeNative(Native Method)
04-02 10:13:07.143: E/AndroidRuntime(849):  at java.lang.reflect.Method.invoke(Method.java:511)
04-02 10:13:07.143: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-02 10:13:07.143: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-02 10:13:07.143: E/AndroidRuntime(849):  at dalvik.system.NativeStart.main(Native Method)
04-02 10:13:07.143: E/AndroidRuntime(849): Caused by: java.lang.reflect.InvocationTargetException
04-02 10:13:07.143: E/AndroidRuntime(849):  at java.lang.reflect.Constructor.constructNative(Native Method)
04-02 10:13:07.143: E/AndroidRuntime(849):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
04-02 10:13:07.143: E/AndroidRuntime(849):  ... 22 more
04-02 10:13:07.143: E/AndroidRuntime(849): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x1/d=0x7f060001 a=-1 r=0x7f060001}
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.content.res.Resources.loadColorStateList(Resources.java:2074)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.widget.TextView.<init>(TextView.java:904)
04-02 10:13:07.143: E/AndroidRuntime(849):  at android.widget.TextView.<init>(TextView.java:578)
04-02 10:13:07.143: E/AndroidRuntime(849):  ... 25 more

片段二

package com.example.fragmentex;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater,
                  ViewGroup container, Bundle savedInstanceState) {

                   //Inflate the layout for this fragment

              return inflater.inflate(
                      R.layout.fragment_two, container, false);
           }  

}

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am in Fragment 2"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@style/AppTheme" />

</LinearLayout>
老鼠来了料理鼠王

选项1:

您的包裹名称为:

com.example.fragmentex

您确定片段在包装中吗?

com.javacodegeeks.android.fragmentstest

检查FragmentOne类存在的位置并复制包名称,然后在xml文件中提及它。

选项2:

尝试将活动更改为FragmentActivity

那是 extends FragmentActivity

编辑:

改变这个:

<fragment
    android:id="@+id/fragment1"
    android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.84" />

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.fragmentex.FragmentOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.84" />

编辑: [对于第二个异常,找不到资源]

改变 :

android:textColor="@style/AppTheme"

到:

style="@style/AppTheme" -> if you want to set the style to the textView

或者

android:textColor = "@color/your_defined_color" - > if you want to set the color to the textView

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

zxing运行时异常到Android应用程序

来自分类Dev

zxing运行时异常到Android应用程序

来自分类Dev

使用DTS运行时的VB应用程序中未处理的异常

来自分类Dev

从启动应用程序运行时,使用通知发送的脚本拒绝

来自分类Dev

获取当前应用程序的运行时间

来自分类Dev

获取当前应用程序的运行时间

来自分类Dev

运行应用程序时出现运行时错误,原因是Qlable

来自分类Dev

运行Spring Session + Spring Boot应用程序时发生运行时错误

来自分类Dev

运行Android应用程序时出现运行时错误

来自分类Dev

DirectX 11:运行简单的DirectX应用程序时运行时崩溃

来自分类Dev

如何使用New Relic API获取应用程序正常运行时间报告?

来自分类Dev

关闭应用程序时获取异常

来自分类Dev

构建Android应用程序时遇到运行时错误

来自分类Dev

Java 运行时,在关闭应用程序时执行一些操作

来自分类Dev

在模拟器上执行应用程序时出现 Android 运行时错误

来自分类Dev

运行时应用程序

来自分类Dev

当应用程序在后台运行时,从通知中打开片段

来自分类Dev

当应用程序在后台运行时,ViewPager片段会被破坏吗?

来自分类Dev

当应用程序在后台运行时,ViewPager片段会被破坏吗?

来自分类Dev

当应用程序不在设备上运行时,FCM打开活动和调用片段

来自分类Dev

Android应用程序中的运行时异常:无法膨胀MapFragment

来自分类Dev

Zxing库应用程序集成运行时异常ClassNotFoundException

来自分类Dev

Android Robotium测试,运行时异常无法从主应用程序线程调用此方法

来自分类Dev

在Amazon AWS上启动Spring Boot应用程序,如何设置目标运行时

来自分类Dev

在应用程序运行时随时启动Visual Studio调试器

来自分类Dev

Docker:无法启动服务应用程序:OCI运行时创建失败:

来自分类Dev

在iOS 7.1上运行时,应用程序无法全屏启动吗?

来自分类Dev

当应用程序运行时,Android 不启动活动(在 IntentFilter 匹配上)

来自分类Dev

分发运行时应用程序时,更改Access使用的默认加密类型是否会引起兼容性问题?

Related 相关文章

  1. 1

    zxing运行时异常到Android应用程序

  2. 2

    zxing运行时异常到Android应用程序

  3. 3

    使用DTS运行时的VB应用程序中未处理的异常

  4. 4

    从启动应用程序运行时,使用通知发送的脚本拒绝

  5. 5

    获取当前应用程序的运行时间

  6. 6

    获取当前应用程序的运行时间

  7. 7

    运行应用程序时出现运行时错误,原因是Qlable

  8. 8

    运行Spring Session + Spring Boot应用程序时发生运行时错误

  9. 9

    运行Android应用程序时出现运行时错误

  10. 10

    DirectX 11:运行简单的DirectX应用程序时运行时崩溃

  11. 11

    如何使用New Relic API获取应用程序正常运行时间报告?

  12. 12

    关闭应用程序时获取异常

  13. 13

    构建Android应用程序时遇到运行时错误

  14. 14

    Java 运行时,在关闭应用程序时执行一些操作

  15. 15

    在模拟器上执行应用程序时出现 Android 运行时错误

  16. 16

    运行时应用程序

  17. 17

    当应用程序在后台运行时,从通知中打开片段

  18. 18

    当应用程序在后台运行时,ViewPager片段会被破坏吗?

  19. 19

    当应用程序在后台运行时,ViewPager片段会被破坏吗?

  20. 20

    当应用程序不在设备上运行时,FCM打开活动和调用片段

  21. 21

    Android应用程序中的运行时异常:无法膨胀MapFragment

  22. 22

    Zxing库应用程序集成运行时异常ClassNotFoundException

  23. 23

    Android Robotium测试,运行时异常无法从主应用程序线程调用此方法

  24. 24

    在Amazon AWS上启动Spring Boot应用程序,如何设置目标运行时

  25. 25

    在应用程序运行时随时启动Visual Studio调试器

  26. 26

    Docker:无法启动服务应用程序:OCI运行时创建失败:

  27. 27

    在iOS 7.1上运行时,应用程序无法全屏启动吗?

  28. 28

    当应用程序运行时,Android 不启动活动(在 IntentFilter 匹配上)

  29. 29

    分发运行时应用程序时,更改Access使用的默认加密类型是否会引起兼容性问题?

热门标签

归档