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

时间:2014-04-01 03:33:29

标签: android android-intent android-fragments

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

在我实现示例Fragment应用程序时,我在Emulator中启动应用程序时会收到 RunTimeException

Comeplete Exception 堆栈如下所示::

    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

FragmentTwo.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 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>

2 个答案:

答案 0 :(得分:0)

看来你的FragmentOne课程不合适。

确保类名存在,是公共的,并且具有公开的空构造函数

public class FragmentOne extends Fragment{

public FragmentOne() { <--- Add this
}

...

}

答案 1 :(得分:0)

选项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