我对此有点动摇,所以请耐心等待。我正在尝试自学,目前正在研究 WROX Android 4 Application Development for Beginners 这本书。我目前在“片段之间的通信”部分,我觉得我明白了,但是当我运行从书中取出的代码时,我在 logcat 中得到“膨胀失败”。这是 XMLs 后跟 .java 文件,然后是错误。我不知道是什么杀死了这个:
主.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>
片段1.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>
片段2.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>
片段活动.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);
}
}
片段1.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);
}
}
片段2.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();
}
});
}
}
日志猫:
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