public class OrtaMain extends Fragment {
private ListView listView;
private int[] user_poster_resources={
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home
};
private int[] alt_resource={
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home
};
private String[] konu_basliks;
private String[] konu_tarihs;
private String[] konu_iceriks;
OrtaMainAdapter adapter;
public OrtaMain() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orta_main, container, false);
listVie = (ListView) view.findViewById(R.id.orta_main_liste);
konu_basliks = getResources().getStringArray(R.array.strin_baslik);
konu_tarihs = getResources().getStringArray(R.array.konu_tarih);
konu_iceriks = getResources().getStringArray(R.array.konu_icerik);
int i = 0;
adapter = new OrtaMainAdapter(getActivity(), R.layout.ortamain_item);
listView.setAdapter(adapter);
for (String basliklar:konu_basliks) {
OrtaMainDataProvider dataProvider = new OrtaMainDataProvider(basliklar);
adapter.add(dataProvider);
i++;
}
return view;
}
}
对片段不起作用,我尝试了一些代码,但是它继续说 - "应用已停止"
我的主要活动
public class OrtaMainDataProvider {
private String baslik;
public OrtaMainDataProvider(String baslik) {
this.setBaslik(baslik);
}
public String getBaslik() {
return baslik;
}
public void setBaslik(String baslik) {
this.baslik = baslik;
}
}
数据提供商
public class OrtaMainAdapter extends ArrayAdapter {
List list = new ArrayList();
public OrtaMainAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
static class DataHandler{
TextView konu_baslik;
}
public int getCount(){
return this.list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView == null){
LayoutInflater inflater =(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.ortamain_item, parent, false);
handler = new DataHandler();
handler.konu_baslik = (TextView) row.findViewById(R.id.orta_main_baslik);
row.setTag(handler);
}
else{
handler = (DataHandler) row.getTag();
}
OrtaMainDataProvider dataProvider;
dataProvider= (OrtaMainDataProvider) this.getItem(position);
handler.konu_baslik.setText(dataProvider.getBaslik());
return row;
}
}
适配器类
06-27 14:31:37.836 11336-11336/com.example.cc.trafikprojesi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cc.trafikprojesi, PID: 11336
java.lang.OutOfMemoryError: Failed to allocate a 80281612 byte allocation with 16777216 free bytes and 42MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
和 logcat信息
ListView
这些代码适用于任何简单的活动。当我测试他人时。但当我把这些代码,listview不起作用,模拟器停止..
此外,当我没有放置ListView
代码时,片段效果很好,而片段给出错误并且在模拟器上不起作用。
SVD
对片段不起作用吗?我必须只放在非碎片上吗?
答案 0 :(得分:0)
这是我的建议,它应该有帮助...
您可以忽略 不推荐 的问题,并通过将其添加到您的Android清单文件来增加heapSize:
<application android:largeHeap="true"
同时设置Bitmap.Config
类型RGB_565
而不是ARGB_8888
,以使位图更小。
但是,这里最好的选择是花一些时间并使用LeakCanary对您的应用进行分析,并观看此视频以了解Android的内存管理 - Google I/O 2011: Memory management for Android Apps(从{{3}开始播放看看为什么largeHeap=true
不好。)然后在4:12他们会详细了解堆使用情况。
大堆修复它是一个临时修复,不应该被认为是最终的。我强烈建议进行性能分析,看看8:31在完全使用Fragment之前调用的方法是onDetach()
方法,您可能希望在此处释放所有资源。片段本身被破坏和gc&ed;这并不意味着你分配的所有资源都被删除了。