我尝试了解如何在Fragments的帮助下创建活动的方式。 所以我编写了一个小例子来熟悉这个问题。
在我的设备上运行时,它总会崩溃,我根本不理解。
我知道问题是由onCreatingView()引起的:当我将其标记为注释时,不会出现错误并且onCreate()将被执行。
有人可以解释应用程序崩溃的原因吗?
提前致谢
以下是我的代码的相关摘要
OverviewActivity.java:
public class OverviewActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.overview, menu);
return true;
}
}
activity_overview:
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overview"
android:name="de.jr.dateoverview.OverviewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
OverviewFragment.java:
public class OverviewFragment extends ListFragment{
private ListAdapter mAdapter;
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setEmptyText(getResources().getString(R.string.noEntry));
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin" };
mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.overview_tile, R.id.tile1, items);
setListAdapter(mAdapter);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.overview_tile, container, false);
}
}
overview_tile:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tile1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"/>
logcat的:
03-29 17:47:23.876: D/AndroidRuntime(8773): Shutting down VM
03-29 17:47:23.876: W/dalvikvm(8773): threadid=1: thread exiting with uncaught exception (group=0x415b8ba8)
03-29 17:47:23.876: E/AndroidRuntime(8773): FATAL EXCEPTION: main
03-29 17:47:23.876: E/AndroidRuntime(8773): Process: de.jr.dateoverview, PID: 8773
03-29 17:47:23.876: E/AndroidRuntime(8773): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.jr.dateoverview/de.jr.dateoverview.OverviewActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.os.Looper.loop(Looper.java:136)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 17:47:23.876: E/AndroidRuntime(8773): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 17:47:23.876: E/AndroidRuntime(8773): at java.lang.reflect.Method.invoke(Method.java:515)
03-29 17:47:23.876: E/AndroidRuntime(8773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 17:47:23.876: E/AndroidRuntime(8773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 17:47:23.876: E/AndroidRuntime(8773): at dalvik.system.NativeStart.main(Native Method)
03-29 17:47:23.876: E/AndroidRuntime(8773): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-29 17:47:23.876: E/AndroidRuntime(8773): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.Activity.setContentView(Activity.java:1929)
03-29 17:47:23.876: E/AndroidRuntime(8773): at de.jr.dateoverview.OverviewActivity.onCreate(OverviewActivity.java:16)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.Activity.performCreate(Activity.java:5231)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 17:47:23.876: E/AndroidRuntime(8773): ... 11 more
03-29 17:47:23.876: E/AndroidRuntime(8773): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
03-29 17:47:23.876: E/AndroidRuntime(8773): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
03-29 17:47:23.876: E/AndroidRuntime(8773): ... 20 more
答案 0 :(得分:2)
您可以从logcat输出中了解错误。
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
您正在创建ListFragment,并且您的布局中没有任何Listview。定义listFragment的listview并将其ID设置为android.R.id.list
答案 1 :(得分:1)
好的,如果你以编程方式添加该片段并且已经在xml中定义了,那就错了。或者已经在你的容器中拥有它或者在运行时添加它,而不是两者都
03-29 17:47:23.876: E/AndroidRuntime(8773): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
在这里你需要确保你的llistview的id就是这样:'android.R.id.list'
答案 2 :(得分:-1)
public class OverviewFragment扩展ListFragment { private ListAdapter mAdapter;
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setEmptyText(getResources().getString(R.string.noEntry));
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.overview_tile, container, false); String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin" };
mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.overview_tile, R.id.tile1, items);
setListAdapter(mAdapter);
}
}
你只需要onCreateView来获取不是onCreate
的片段