android – 无法实例化片段,确保类名存在,是public,并且有一个空的构造函数是public

前端之家收集整理的这篇文章主要介绍了android – 无法实例化片段,确保类名存在,是public,并且有一个空的构造函数是public前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用片段,当我改变设备的方向时.如果最初它的肖像,当我改变为景观,那么我的应用程序崩溃.我在这里添加了logcat.我经历了很多环节,但找不到正确的答案.

请帮我解决这个问题.

谢谢

public class PageViewActivity extends FragmentActivity {

    private ViewPager viewPager;  
    private NoticePageAdapter noticePageAdapter;
    private TextView titleText;
    private int pageIndex;
    private static int restTime = 0;
    private long lastTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_page_view);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title2);
        titleText = (TextView) findViewById(R.id.title2);
        titleText.setText(R.string.app_name);

        // Create the adapter that will return a fragment for each of the three  
        // primary sections of the app.  
        noticePageAdapter = new NoticePageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.  
        viewPager = (ViewPager) findViewById(R.id.viewpager);  
        Intent intent = getIntent();
        pageIndex = intent.getIntExtra("notice_position",0);
        viewPager.setAdapter(noticePageAdapter);
        viewPager.setCurrentItem(pageIndex,true);

        lastTime = System.currentTimeMillis();

        //Check the rest time. If it exceed the 30 sec then finish the activity.
        new CheckTimeThread().start();
    }

    /** 
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
     * one of the sections/tabs/pages. 
     */  
    class NoticePageAdapter extends FragmentPagerAdapter {  

        public NoticePageAdapter(FragmentManager fm) {  
            super(fm);  
        }  

        @Override  
        public Fragment getItem(int position) {  
            Fragment fragment = new NoticeFragment();
            Bundle args = new Bundle();
            args.putInt(NoticeFragment.TEMPLATE_POSITION,position + 1);  
            fragment.setArguments(args);  
            return fragment;
        }  

        @Override  
        public int getCount() {  
            return NoticeData.templateId.length;  
        }  
    }  

    /** 
     * A Notice fragment representing a notices of the app,but that simply 
     * displays notices 
     */  
    public class NoticeFragment extends Fragment { 
        public static final String TEMPLATE_POSITION = "template_position";
        private TextView noticeHeaderTextView;
        private TextView noticeContentTextView;
        private ImageView noticeImageView;

        @Override  
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {  
            int templatePosition = getArguments().getInt(TEMPLATE_POSITION);
            int templateId = 0;
            int tempVar;

            tempVar = templatePosition - 1;
            templateId = Integer.parseInt(NoticeData.templateId[tempVar]);

            int res = R.layout.first_template;

            switch (templateId) {
            case 1:
                res = R.layout.first_template;
                break;
            case 2:
                res = R.layout.second_template;
                break;
            case 3:
                res = R.layout.third_template;
                break;
            case 4:
                res = R.layout.fourth_template;
                break;
            default:
                break;
            }

            View rootView = inflater.inflate(res,container,false);
            noticeHeaderTextView = (TextView)rootView.findViewById(R.id.noticeHeading);
            noticeHeaderTextView.setText(Html.fromHtml(NoticeData.noticeHeading[tempVar]));

            noticeContentTextView = (TextView)rootView.findViewById(R.id.noticeContent);
            noticeContentTextView.setText(Html.fromHtml(NoticeData.noticeContent[tempVar]));

            noticeImageView = (ImageView)rootView.findViewById(R.id.noticeImageView);
            UrlImageViewHelper.setUrlDrawable(noticeImageView,NoticeData.imagesURL[tempVar]);

            DisplayMetrics metrics = getResources().getDisplayMetrics();
            int width = metrics.widthPixels;

            if(templateId == 3) {
                noticeImageView.getLayoutParams().width = width / 2;
            }
            else if(templateId == 2) {
                noticeHeaderTextView.getLayoutParams().width = width/2;
                noticeContentTextView.getLayoutParams().width = width/2;
            }

            RelativeLayout relativeLayout = (RelativeLayout)rootView.findViewById(R.id.relativeLayout);
            relativeLayout.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v,MotionEvent event) {
                    resetRestTime();
                    return false;
                }
            });

            return rootView;  
        }  
    }
}

错误跟踪:

06-24 18:24:36.501: E/AndroidRuntime(11863): FATAL EXCEPTION: main
06-24 18:24:36.501: E/AndroidRuntime(11863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noticeboard/com.noticeboard.PageViewActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.noticeboard.PageViewActivity$NoticeFragment: make sure class name exists,is public,and has an empty constructor that is public
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.access$700(ActivityThread.java:128)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1165)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.os.Looper.loop(Looper.java:137)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.main(ActivityThread.java:4514)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at java.lang.reflect.Method.invoke(Method.java:511)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at dalvik.system.NativeStart.main(Native Method)
06-24 18:24:36.501: E/AndroidRuntime(11863): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.noticeboard.PageViewActivity$NoticeFragment: make sure class name exists,and has an empty constructor that is public
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at com.noticeboard.PageViewActivity.onCreate(PageViewActivity.java:40)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.Activity.performCreate(Activity.java:4465)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
06-24 18:24:36.501: E/AndroidRuntime(11863):    ... 12 more
06-24 18:24:36.501: E/AndroidRuntime(11863): Caused by: java.lang.InstantiationException: can't instantiate class com.noticeboard.PageViewActivity$NoticeFragment; no empty constructor
06-24 18:24:36.501: E/AndroidRuntime(11863):    at java.lang.Class.newInstanceImpl(Native Method)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at java.lang.Class.newInstance(Class.java:1319)
06-24 18:24:36.501: E/AndroidRuntime(11863):    at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
06-24 18:24:36.501: E/AndroidRuntime(11863):    ... 19 more

解决方法

我把内部的类静态化了.
public static class NoticeFragment extends Fragment {

解决了我的问题.

原文链接:https://www.f2er.com/android/312027.html

猜你在找的Android相关文章