2013-06-24 2 views
10

私はフラグメントを使用しています。最初はその肖像画と私は風景に変更するとき、私のアプリケーションがクラッシュします。私は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, 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 

答えて

16

私は静的に内部クラスを作りました。

public static class NoticeFragment extends Fragment { 

私の問題を解決しました。

+0

私は静的にしましたが、解決しませんでした。 'public'クラスに分割してもエラーが出ます(私はFragmentPagerAdapterを使用しています) –

1

getItem関数では、関数を使用してフラグメントを作成しないでください。newInstance関数を使用してください。

関連する問題