无法实例片段确保类名称存在,是公开的,并且有一个空的构造是公有一个、片段、实例、名称

2023-09-04 11:12:57 作者:想看海

我使用的片段,当我改变设备的方向。如果最初的画像,当我将其更改为横向,然后我的应用程序崩溃。我在这里添加的logcat的。我已经经过许多环节走了,但找不到正确的答案。

请帮我解决这个问题。

感谢

 公共类PageViewActivity扩展FragmentActivity {

    私人ViewPager viewPager;
    私人NoticePageAdapter noticePageAdapter;
    私人TextView的titleText;
    私人诠释PageIndex的;
    私有静态诠释restTime = 0;
    专用长lastTime;

    @覆盖
    公共无效的onCreate(包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);

        //创建将返回一个片段为三个的适配器
        //应用程序的主要部分。
        noticePageAdapter =新NoticePageAdapter(getSupportFragmentManager());

        //设置的ViewPager与部分适配器。
        viewPager =(ViewPager)findViewById(R.id.viewpager);
        意向意图= getIntent();
        的PageIndex = intent.getIntExtra(notice_position,0);
        viewPager.setAdapter(noticePageAdapter);
        viewPager.setCurrentItem(PageIndex的,真正的);

        lastTime = System.currentTimeMillis的();

        //检查的休息时间。如果超过30秒,然后完成活动。
        新CheckTimeThread()启动();
    }

    / **
     * A {@link FragmentPagerAdapter}返回对应的片段
     *章节/标签/页之一。
     * /
    类NoticePageAdapter扩展FragmentPagerAdapter {

        公共NoticePageAdapter(FragmentManager FM){
            超(FM);
        }

        @覆盖
        公共片段的getItem(INT位置){
            片段片段=新NoticeFragment();
            捆绑的args =新包();
            args.putInt(NoticeFragment.TEMPLATE_POSITION,位置+ 1);
            fragment.setArguments(参数);
            返回片段;
        }

        @覆盖
        公众诠释getCount将(){
            返回NoticeData.templateId.length;
        }
    }

    / **
     *的通知片段重新presenting应用程序的通知,但只是
     *显示通知
     * /
    公共类NoticeFragment扩展片段{
        公共静态最后弦乐TEMPLATE_POSITION =template_position;
        私人TextView的noticeHeaderTextView;
        私人TextView的noticeContentTextView;
        私人ImageView的noticeImageView;

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑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;

            开关(templateId){
            情况1:
                RES = R.layout.first_template;
                打破;
            案例2:
                RES = R.layout.second_template;
                打破;
            案例3:
                RES = R.layout.third_template;
                打破;
            壳体4:
                RES = R.layout.fourth_template;
                打破;
            默认:
                打破;
            }

            查看rootView = inflater.inflate(RES,集装箱,假);
            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指标= getResources()getDisplayMetrics()。
            INT宽度= metrics.widthPixels;

            如果(templateId == 3){
                。noticeImageView.getLayoutParams()宽度=宽度/ 2;
            }
            否则,如果(templateId == 2){
                。noticeHeaderTextView.getLayoutParams()宽度=宽度/ 2;
                。noticeContentTextView.getLayoutParams()宽度=宽度/ 2;
            }

            RelativeLayout的RelativeLayout的=(RelativeLayout的)rootView.findViewById(R.id.relativeLayout);
            relativeLayout.setOnTouchListener(新OnTouchListener(){
                @覆盖
                公共布尔onTouch(视图V,MotionEvent事件){
                    resetRestTime();
                    返回false;
                }
            });

            返回rootView;
        }
    }
}
 

错误跟踪:

  06-24 18:24:36.501:E / AndroidRuntime(11863):致命异常:主要
06-24 18:24:36.501:E / AndroidRuntime(11863):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.noticeboard / com.noticeboard.PageViewActivity}:android.support.v4.app.Fragment $ InstantiationException :无法实例片段com.noticeboard.PageViewActivity $ NoticeFragment:确保类名称存在,是公开的,并且有一个空的构造是公
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.access $ 700(ActivityThread.java:128)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1165)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.os.Handler.dispatchMessage(Handler.java:99)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.os.Looper.loop(Looper.java:137)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.main(ActivityThread.java:4514)
06-24 18:24:36.501:E / AndroidRuntime(11863):在java.lang.reflect.Method.invokeNative(本机方法)
06-24 18:24:36.501:E / AndroidRuntime(11863):在java.lang.reflect.Method.invoke(Method.java:511)
06-24 18:24:36.501:E / AndroidRuntime(11863):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
06-24 18:24:36.501:E / AndroidRuntime(11863):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-24 18:24:36.501:E / AndroidRuntime(11863):在dalvik.system.NativeStart.main(本机方法)
06-24 18:24:36.501:E / AndroidRuntime(11863):android.support.v4.app.Fragment $ InstantiationException:产生的原因无法实例片段com.noticeboard.PageViewActivity $ NoticeFragment:确保类名称存在,是公众,并且有一个空的构造是公
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.support.v4.app.Fragment.instantiate(Fragment.java:399)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
06-24 18:24:36.501:E / AndroidRuntime(11863):在com.noticeboard.PageViewActivity.onCreate(PageViewActivity.java:40)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.Activity.performCreate(Activity.java:4465)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
06-24 18:24:36.501:E / AndroidRuntime(11863):12 ...更多
06-24 18:24:36.501:E / AndroidRuntime(11863):java.lang.InstantiationException:产生的原因无法实例类com.noticeboard.PageViewActivity $ NoticeFragment;没有空的构造
06-24 18:24:36.501:E / AndroidRuntime(11863):在java.lang.Class.newInstanceImpl(本机方法)
06-24 18:24:36.501:E / AndroidRuntime(11863):在java.lang.Class.newInstance(Class.java:1319)
06-24 18:24:36.501:E / AndroidRuntime(11863):在android.support.v4.app.Fragment.instantiate(Fragment.java:388)
06-24 18:24:36.501:E / AndroidRuntime(11863):19 ...更多
 
JavaScript高级 ES6

解决方案

我做了内部类是静态的。

 公共静态类NoticeFragment扩展片段{
 

这解决了我的问题。

I am using the Fragment and When I change the orientation of the device. If initially its portrait and when i change it to landscape then my application crash. I added the logcat here. I have gone through many links but could not find the right answer.

Please help me to solve this issue.

Thanks

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;  
        }  
    }
}

Error Trace:

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, is public, 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

解决方案

I made the inner class to static.

public static class NoticeFragment extends Fragment { 

It solved my problem.