我是android的新手,我面临着一个令人沮丧的挑战,我不确定如何应对。我正在尝试创建一个使用两个片段的应用程序,一个片段列表将列出城市,另一个片段列表将显示有关所选城市的信息。我主要遇到了致命的异常。
我向我的教授询问了发生了什么事,他看了我的项目约20分钟,也弄不清这是怎么回事。我曾尝试注释掉我认为可能对它有所帮助的部分代码,但我认为问题与我的xml有关,但我对它们还不是很熟悉。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onListFragmentInteraction(DummyContent.DummyItem item){
Log.v("idk", "item: \n" + item.details);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CityFragment cityFragment = (CityFragment) fragmentManager.findFragmentById(R.id.cityListFragment);
TextFragment textFragment = (TextFragment) fragmentManager.findFragmentById(R.id.textFragment);
fragmentTransaction.add(R.id.cityListFragment, cityFragment);
fragmentTransaction.commit();
fragmentTransaction.add(R.id.textFragment, textFragment);
fragmentTransaction.commit();
}
@Override
public void onFragmentInteraction(Uri uri){
}
}
这是我的主要活动
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:id="@+id/view"
style="@style/Divider"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</View>
<fragment
android:id="@+id/cityListFragment"
android:name="com.example.tourguide.CityFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/textFragment"
android:name="com.example.tourguide.TextFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
</android.support.constraint.ConstraintLayout>
这是我的主要xml文件
Process: com.example.tourguide, PID: 15175
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourguide/com.example.tourguide.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:349)
at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:150)
at android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1695)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1431)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1930)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3745)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.tourguide.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-05-22 19:25:04.806 15175-15175/com.example.tourguide E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-05-22 19:25:04.819 15175-15175/com.example.tourguide I/Process: Sending signal. PID: 15175 SIG: 9
这是我的Logcat。
我为大量代码表示歉意,或者如果我事先犯了一个非常基本的错误,但是我花了很多时间试图自己解决这个问题,结果更加困惑。 感谢您的帮助!
答案 0 :(得分:1)
第23行的内容为:goto com.example.tourguide.CityFragment
并将resId android.R.id.list
添加到该片段中的任何ListView
(无论是从XML
还是由Java
构造的)。
答案 1 :(得分:0)
首先,您应该将片段的宽度和高度设置为
android:layout_width="match_parent"
android:layout_height="xxdp"