Fragment强烈不推荐使用自定义带参的构造函数

项目在运行monkey63小时左右,出现9次CRASH:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aliyun.easylauncher/com.aliyun.easylauncher.dialpad.PhoneRecordActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.aliyun.easylauncher.dialpad.DialFragment: make sure class name exists, is public, and has an empty constructor that is public

正常的使用一切都是正常,Google后发现,Fragment有下面的注意事项:

public Fragment ()

Default constructor. Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment withgetArguments().

Applications should generally not implement a constructor. The first place application code an run where the fragment is ready to be used is in onAttach(Activity), the point where the fragment is actually associated with its activity. Some applications may also want to implementonInflate(Activity, AttributeSet, Bundle) to retrieve attributes from a layout resource, though should take care here because this happens for the fragment is attached to its activity.

看了一下frameworks/support/v4/java/android/support/v4/app/Fragment.java(因为import android.support.v4.app.Fragment)


 394     public static Fragment instantiate(Context context, String fname, Bundle args) {
   395         try {                    
   396             Class<?> clazz = sClassMap.get(fname);   
   397             if (clazz == null) { 
   398                 // Class not found in the cache, see if it's real, and try to add it
   399                 clazz = context.getClassLoader().loadClass(fname);
   400                 sClassMap.put(fname, clazz);             
   401             }                    
   402             Fragment f = (Fragment)clazz.newInstance(); //问题出在这里
   403             if (args != null) {  
   404                 args.setClassLoader(f.getClass().getClassLoader());
   405                 f.mArguments = args;
   406             }                    
   407             return f;            
   408         } catch (ClassNotFoundException e) {     
   409             throw new InstantiationException("Unable to instantiate fragment " + fname
   410                     + ": make sure class name exists, is public, and has an"
   411                     + " empty constructor that is public", e); 
   412         } catch (java.lang.InstantiationException e) {
   413             throw new InstantiationException("Unable to instantiate fragment " + fname
   414                     + ": make sure class name exists, is public, and has an"
   415                     + " empty constructor that is public", e); 
   416         } catch (IllegalAccessException e) {
   417             throw new InstantiationException("Unable to instantiate fragment " + fname
   418                     + ": make sure class name exists, is public, and has an"
   419                     + " empty constructor that is public", e);
   420         }
   421     

解决方案(来自stackoverflow),原文链接: http://stackoverflow.com/questions/10450348/do-fragments-really-need-an-empty-constructor


You shouldn't really be overriding the constructor anyway. You should have a newInstance() static method defined and pass any parameters via arguments (bundle)

For example:

public static final AlertFragment newInstance(int title, String message)
{
    AlertFragment f = new AlertFragment();
    Bundle bdl = new Bundle(2);
    bdl.putInt(EXTRA_TITLE, title);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}

And of course grabbing the args this way:

@Override
public void onCreate(Bundle savedInstanceState)
{
    title = getArguments().getInt(EXTRA_TITLE);
    message = getArguments().getString(EXTRA_MESSAGE);

    //...
    //etc
    //...
}

Then you would instantiate from your fragment manager like so:

public onCreate(Bundle savedInstanceState) {
    if(savedInstanceState == null){
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content,AlertFragment.newInstance(
                R.string.alert_title,
                "Oh noes an error occured!")
            )
            .commit();
    }
}

This way if detached and re-attached the object state can be stored through the arguments. Much like bundles attached to Intents.



Android开发中,我们可以使用ListView来显示一个垂直滚动的列表。如果我们想要自定义ListView的每一个item的布局,我们可以使用自定义的Adapter来实现。 以下是使用自定义Adapter实现自定义ListView的步骤: 1. 创建一个layout文件,用于定义每一个ListView item的布局。例如,我们可以创建一个名为list_item.xml的文件,其中包含我们想要显示的视图元素。 2. 创建一个自定义Adapter类,继承自BaseAdapter。在这个类中,我们需要实现以下方法: * getCount():返回ListView中项的数量 * getItem():返回指定位置上的数据对象 * getItemId():返回指定位置上的数据对象的ID * getView():返回自定义的ListView item的视图 3. 在Activity或Fragment中,创建一个ListView对象,并设置Adapter为自定义的Adapter。 下面是一个简单的例子: 1. list_item.xml文件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <ImageView android:id="@+id/imageView" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@android:color/black" android:layout_marginLeft="16dp"/> </LinearLayout> ``` 2. 自定义Adapter类: ```java public class MyListAdapter extends BaseAdapter { private List<MyItem> mData; public MyListAdapter(List<MyItem> data) { mData = data; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.imageView = convertView.findViewById(R.id.imageView); holder.textView = convertView.findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } MyItem item = mData.get(position); holder.imageView.setImageResource(item.getImageResId()); holder.textView.setText(item.getText()); return convertView; } private static class ViewHolder { ImageView imageView; TextView textView; } } ``` 其中,MyItem是一个自定义的数据类,包含了需要在ListView中显示的数据。 3. 在Activity或Fragment使用ListView和Adapter: ```java public class MyFragment extends Fragment { private ListView mListView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); mListView = view.findViewById(R.id.listView); List<MyItem> data = new ArrayList<>(); data.add(new MyItem(R.drawable.ic_launcher, "Item 1")); data.add(new MyItem(R.drawable.ic_launcher, "Item 2")); data.add(new MyItem(R.drawable.ic_launcher, "Item 3")); MyListAdapter adapter = new MyListAdapter(data); mListView.setAdapter(adapter); return view; } } ``` 这样,我们就完成了一个简单的自定义ListView的实现。在实际开发中,我们可以根据需求对ListView的布局进行更加复杂的自定义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值