这是我的片段类
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CommunityFragment extends Fragment {
public CommunityFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
return rootView;
}
}
以下是其简单的 xml ,其中当我尝试添加 listview标记时,会出现错误
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_marginTop="10dp"
android:background="@drawable/bg"
android:divider="#9c9c9c"
android:dividerHeight="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
这是StackTrace
12-30 20:42:18.572: E/AndroidRuntime(940): FATAL EXCEPTION: main
12-30 20:42:18.572: E/AndroidRuntime(940): android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
12-30 20:42:18.572: E/AndroidRuntime(940): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-30 20:42:18.572: E/AndroidRuntime(940): at info.androidhive.slidingmenu.CommunityFragment.onCreateView(CommunityFragment.java:36)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.os.Handler.handleCallback(Handler.java:605)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.os.Looper.loop(Looper.java:137)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-30 20:42:18.572: E/AndroidRuntime(940): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 20:42:18.572: E/AndroidRuntime(940): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 20:42:18.572: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-30 20:42:18.572: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-30 20:42:18.572: E/AndroidRuntime(940): at dalvik.system.NativeStart.main(Native Method)
12-30 20:42:18.572: E/AndroidRuntime(940): Caused by: java.lang.reflect.InvocationTargetException
12-30 20:42:18.572: E/AndroidRuntime(940): at java.lang.reflect.Constructor.constructNative(Native Method)
12-30 20:42:18.572: E/AndroidRuntime(940): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
12-30 20:42:18.572: E/AndroidRuntime(940): ... 21 more
12-30 20:42:18.572: E/AndroidRuntime(940): Caused by: java.lang.OutOfMemoryError
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.content.res.Resources.loadDrawable(Resources.java:1937)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.View.<init>(View.java:2780)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.view.ViewGroup.<init>(ViewGroup.java:385)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.widget.AdapterView.<init>(AdapterView.java:234)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.widget.AbsListView.<init>(AbsListView.java:734)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.widget.ListView.<init>(ListView.java:141)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.widget.ListView.<init>(ListView.java:137)
12-30 20:42:18.572: E/AndroidRuntime(940): ... 24 more
是否可以在片段类中添加listview? 希望有人可以帮助我,谢谢你!
答案 0 :(得分:1)
我想发表评论,但没有足够的声誉...... 从你的堆栈跟踪看起来我的问题是在加载listview的背景时,这个图像可能很大。
...
Caused by: java.lang.OutOfMemoryError
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
12-30 20:42:18.572: E/AndroidRuntime(940): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
...
尝试从列表视图中删除背景drawable或用简单的颜色替换它。
答案 1 :(得分:0)
您可以通过以下方式执行此操作 -
public class CommunityFragment extends Fragment {
public CommunityFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
View lv1 = rootView .findViewById(R.id.listView);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> files = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
lv1.setAdapter(files);
return rootView;
}
}