我使用ViewPager在一个片段中有两个片段。
有时我的应用会因以下错误而崩溃:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MyAcitivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.fragments.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.fragments.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.buzzreel.MyActivity.onCreate(MyActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.InstantiationException: can't instantiate class com.fragments.MyFragment; no empty constructor
at java.lang.Class.newInstanceImpl(Class.java)
at java.lang.Class.newInstance(Class.java:1208)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.buzzreel.MyActivity.onCreate(MyActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(NativeStart.java)
但我的片段类中有两个构造函数如下:
public MyFragment(CustomObject custom) {
this.custom = custom;
}
public MyFragment() {
}
我的寻呼机适配器呼叫:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.fragments.MyFragment
public class MyPagerAdapter extends FragmentPagerAdapter {
private String tabtitles[] = new String[] { "Info" };
private CustomOjbect object;
public MyPagerAdapter(FragmentManager fm, CustomOjbect object) {
super(fm);
this.object = object;
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new MyFragment(object);
}
return null;
}
@Override
public int getCount() {
return 1;
}
@Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}
}
片段代码[代码很大,因为其他代码只保留核心代码]
public class MyFragment extends Fragment implements OnClickListener {
/**
Variable declaration for widgets and views comes here
**/
public MyFragment(CustomObject object) {
this.object = object;
}
public MyFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_layout,
container, false);
initializeUIComponents();
/**
...
..
rest of code
**/
return view;
}
private void initializeUIComponents() {
// ui views initialization
}
}
在View pager中,我使用arguements调用构造函数。为什么会失败?
答案 0 :(得分:0)
看起来您的套餐有所不同。
import com.fragment.MyFragment
VS
can't instantiate class com.fragments.MyFragment
答案 1 :(得分:0)
避免使用除了片段的默认非参数构造函数之外的重载构造函数,因为当Android框架决定它需要再次重新创建片段时,将仅调用默认的非参数构造函数。您可以使用setArguments()
和getArguments()
将“构造函数参数”传递给您的片段。
请参阅:http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html