碎片无法充气

时间:2013-04-20 19:18:24

标签: android android-fragments

我对此感到有些不满,所以请耐心等待。我正在努力教自己,我目前正在研究WROX And​​roid 4应用程序开发初学者一书。我目前正处于“碎片之间的通信”部分,我觉得我已经得到了它,但是当我运行从本书中取出的代码时,我在logcat中“无法充气”。这是.java文件后跟的XML,然后是错误。我无法弄清楚是什么在扼杀这个:

main.xml中:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <fragment
        android:name="com.example.Fragments.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        />
    <fragment
        android:name="com.example.Fragments.Fragment2"
        android:id="@+id/fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        />
</LinearLayout>

fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    >
    <TextView 
        android:id="@+id/lblFragment1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textColor="#000000"
        android:textSize="25sp"
        />
</LinearLayout>

fragment2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFE00"
    >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textColor="#000000"
        android:textSize="25sp"
        />
    <Button
        android:id="@+id/btnGetText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get text in Fragment #1"
        android:textColor="#000000"
        android:onClick="onClick"
        />
</LinearLayout>

FragmentsActivity.java:

package com.example.fragments;

import android.app.Activity;
import android.os.Bundle;

public class FragmentsActivity extends Activity {

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

Fragment1.java:

package com.example.fragments;

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

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // ---Inflate the layout for this fragment---
        return inflater.inflate(R.layout.fragment1, container, false);
    }

}

Fragment2.java:

package com.example.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
        //---Inflate the layout for this fragment---
        return inflater.inflate(
                R.layout.fragment2, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        //---Button View---
        Button btnGetText = (Button)
                getActivity().findViewById(R.id.btnGetText);
        btnGetText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                TextView lbl = (TextView)
                        getActivity().findViewById(R.id.lblFragment1);
                Toast.makeText(getActivity(), lbl.getText(), 
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

logcat的:

04-20 15:09:55.759: E/ActivityThread(9523): Failed to inflate
04-20 15:09:55.759: E/ActivityThread(9523): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:282)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.setContentView(Activity.java:1881)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.example.fragments.FragmentsActivity.onCreate(FragmentsActivity.java:15)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.performCreate(Activity.java:5104)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Looper.loop(Looper.java:137)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.main(ActivityThread.java:5195)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.NativeStart.main(Native Method)
04-20 15:09:55.759: E/ActivityThread(9523): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.Fragments.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:592)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:560)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.onCreateView(Activity.java:4709)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-20 15:09:55.759: E/ActivityThread(9523):     ... 21 more
04-20 15:09:55.759: E/ActivityThread(9523): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.Fragments.Fragment1" on path: /data/app/com.example.fragments-2.apk
04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:582)
04-20 15:09:55.759: E/ActivityThread(9523):     ... 24 more

2 个答案:

答案 0 :(得分:4)

布局定义错误android:name="com.example.Fragments.Fragment1"。它应该是

android:name="com.example.fragments.Fragment1"

Fragment2

相同

答案 1 :(得分:1)

看起来您错误输入了包名。

Logcat显示找不到类com.example.Fragments.Fragment1。您的Fragment1源代码显示您将包命名为com.example.fragments而不是com.example.Fragments。大写包名称,或小写对它的引用。