我是Android应用程序开发的新手。我知道我的问题可能很简单。但是,我确信它与布局有关。
这是我的问题:片段中有 listView 。当我单击按钮时,新的片段将添加到主布局中的 ScrollView 。当我开始活动时,一个片段已经添加到活动中。因此,当我更改旋转时,应用程序不会崩溃。
但是,单击按钮后,ScrollView中会添加新的Fragment。但是这次当我更改设备的旋转时,应用程序崩溃,最后给出了错误。你能帮我解决这个问题吗?只有当我更改旋转时才会出现错误,否则应用运行正常。
布局(XML)文件activity_another_custom_list_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_another_custom_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.practices.app.practices.AnotherCustomListViewActivity"
android:background="@drawable/border">
<Button
android:text="@string/add_fragments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="addfrag"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.practices.app.practices.AnotherCustomListViewFragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/fragment"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
片段布局another_custom_listview_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fraglistview"></ListView>
</LinearLayout>
Java文件AnotherCustomListViewActivity.java
package com.practices.app.practices;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
public class AnotherCustomListViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_custom_list_view);
}
public void addfrag(View v){
AnotherCustomListViewFragment f = new AnotherCustomListViewFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment,f);
ft.commit();
}
}
Fragment Java Class AnotherCustomListViewFragment
package com.practices.app.practices;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AnotherCustomListViewFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_custom_listview_fragment, container, false);
String[] str = {"a","b","c","d"};
ListView fraglist = (ListView) view.findViewById(R.id.fraglistview);
ListAdapter listadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, str);
fraglist.setAdapter(listadapter);
return view;
}
}
这是完整的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.practices.app.practices/com.practices.app.practices.AnotherCustomListViewActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
Caused by: java.lang.IllegalStateException: Fragment com.practices.app.practices.AnotherCustomListViewFragment did not create a view.
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
at android.app.Activity.onCreateView(Activity.java:5373)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.practices.app.practices.AnotherCustomListViewActivity.onCreate(AnotherCustomListViewActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6024)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3994)
at android.app.ActivityThread.access$1000(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
我在评论部分添加了对答案的回复,但没有人回答。
答案 0 :(得分:0)
几天前我遇到了这种问题,但是在我的布局中更改了drawable之后它解决了我的问题,因为我的drawable太大了,这就是导致问题的原因。 您可以检查您的布局是否为drawable(如果它比较大)而不是使用其他小布局。 我认为这将结束你的问题。
答案 1 :(得分:0)
就我而言,更新库对我有用。