我在运行此应用时遇到问题,启动后会立即崩溃。我正在练习碎片,我不知道问题出在哪里。我发布错误日志和我的代码。谢谢你的帮助:))
09-27 07:08:43.554: E/AndroidRuntime(1212): FATAL EXCEPTION: main
09-27 07:08:43.554: E/AndroidRuntime(1212): Process: com.example.rssfeed, PID: 1212
09-27 07:08:43.554: E/AndroidRuntime(1212): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rssfeed/com.example.rssfeed.RssfeedActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.os.Handler.dispatchMessage(Handler.java:102)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.os.Looper.loop(Looper.java:136)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-27 07:08:43.554: E/AndroidRuntime(1212): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 07:08:43.554: E/AndroidRuntime(1212): at java.lang.reflect.Method.invoke(Method.java:515)
09-27 07:08:43.554: E/AndroidRuntime(1212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-27 07:08:43.554: E/AndroidRuntime(1212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-27 07:08:43.554: E/AndroidRuntime(1212): at dalvik.system.NativeStart.main(Native Method)
09-27 07:08:43.554: E/AndroidRuntime(1212): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-27 07:08:43.554: E/AndroidRuntime(1212): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Activity.setContentView(Activity.java:1929)
09-27 07:08:43.554: E/AndroidRuntime(1212): at com.example.rssfeed.RssfeedActivity.onCreate(RssfeedActivity.java:12)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Activity.performCreate(Activity.java:5231)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-27 07:08:43.554: E/AndroidRuntime(1212): ... 11 more
09-27 07:08:43.554: E/AndroidRuntime(1212): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.rssfeed.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Fragment.instantiate(Fragment.java:597)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Fragment.instantiate(Fragment.java:561)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Activity.onCreateView(Activity.java:4778)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-27 07:08:43.554: E/AndroidRuntime(1212): ... 21 more
09-27 07:08:43.554: E/AndroidRuntime(1212): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.rssfeed.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.rssfeed-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.rssfeed-2, /system/lib]]
09-27 07:08:43.554: E/AndroidRuntime(1212): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-27 07:08:43.554: E/AndroidRuntime(1212): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-27 07:08:43.554: E/AndroidRuntime(1212): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-27 07:08:43.554: E/AndroidRuntime(1212): at android.app.Fragment.instantiate(Fragment.java:583)
09-27 07:08:43.554: E/AndroidRuntime(1212): ... 24 more
RssfeedActivity
package com.example.rssfeed;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
}
// if the wizard generated an onCreateOptionsMenu you can delete
// it, not needed for this tutorial
@Override
public void onRssItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}
}
}
package com.example.rssfeed;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
MyListFragment
package com.example.rssfeed;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
private OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview,
container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return view;
}
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet MyListFragment.OnItemSelectedListener");
}
}
// May also be triggered from the Activity
public void updateDetail() {
// create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onRssItemSelected(newTime);
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.android.rssfeed.MyListFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssfeed.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</RelativeLayout>
<?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/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Default Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</LinearLayout>
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to update" />
</LinearLayout>
答案 0 :(得分:1)
似乎MyListFragment
和DetailFragment
是com.example.rssfeed
package的一部分。所以Fragment
标记更改
com.example.android.rssfeed.MyListFragment
到
com.example.rssfeed.MyListFragment
和
com.example.android.rssfeed.DetailFragment
到
com.example.rssfeed.DetailFragment
即。在您的布局文件中,更改
fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.android.rssfeed.MyListFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssfeed.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
到
fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.rssfeed.MyListFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.rssfeed.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>