2

Я получаю исключение classcasting, которое заявляет, что RadioButton не может быть передан в ViewGroup при переходе от активности к фрагменту, нажав на RadioButton. Любые предложения, пожалуйста и спасибо?Кастинг RadioButton to ViewGroup

radioFeatures selected 
11-20 12:50:08.794 4339-4339/com.example.justin.myapplication V/endofmain-features﹕ endofmain-features 
11-20 12:50:08.802 4339-4339/com.example.justin.myapplication D/AndroidRuntime﹕ Shutting down VM 
11-20 12:50:08.802 4339-4339/com.example.justin.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40f2a930) 
11-20 12:50:08.809 4339-4339/com.example.justin.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.ClassCastException: android.widget.RadioButton cannot be cast to android.view.ViewGroup 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:945) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) 
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) 
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) 
      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458) 
      at android.os.Handler.handleCallback(Handler.java:725) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5041) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
      at dalvik.system.NativeStart.main(Native Method) 

@Override 
     //protected void onCreate(Bundle savedInstanceState, ViewGroup container){ 
     protected void onCreate(Bundle savedInstanceState) { 

     Log.v("Main.class", "Main.class"); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.button_tabs); 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     mAdapter = new CustomButtonTabs(getSupportFragmentManager()); 
     viewPager.setAdapter(mAdapter); 

     radioFeatures = (RadioButton) findViewById(R.id.radioFeatures); 
     radioFeatures.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View vi) { 
       Log.v("radioFeatures selected", "radioFeatures selected"); 
       FeaturesFragment frag = new FeaturesFragment(); 
       android.support.v4.app.FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction(); 
       fragTransaction.replace(R.id.radioFeatures, frag); 
       fragTransaction.commit(); 
       Log.v("endofmain-features", "endofmain-features"); 
      } 
     }); 

to the following of FeaturesFragment: 

    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      Log.v("FeaturesFragment", "FeaturesFragment"); 

      // View v = (View) inflater.inflate(R.layout.carfeatures_list, container, false); 
      View v = inflater.inflate(R.layout.carfeatures_list, null); 
      ListView lv = (ListView) v.findViewById(android.R.id.list); 

      adapter = new CustomListViewAdapter(carFeaturesList, R.layout.carfeatures_list, super.getActivity()); 
      lv.setAdapter(adapter); 
      v.findViewById(R.id.featureslist); 

      //Listview on item click listener 
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        //Gets values from selected ListItem 

        String car_features = ((TextView) view.findViewById(R.id.car_features)).getText().toString(); 
        //Log.v("onCreate-car_features", car_features.toString()); 

        String carfeatures_id = ((TextView) view.findViewById(R.id.carfeatures_id)).getText().toString(); 
        //Log.v("onCreate-carfeatures_id", carfeatures_id.toString()); 

        String model_id = ((TextView) view.findViewById(R.id.model_id)).getText().toString(); 
        //Log.v("onCreate-model_id", model_id.toString()); 

        String carfeatures_desc = ((TextView) view.findViewById(R.id.carfeatures_desc)).getText().toString(); 
        //Log.v("onCreate-carfeats_desc", carfeatures_desc.toString()); 

        Boolean b = Boolean.valueOf(TAG_CARID); 
        if (b == true) { 
         view.setBackgroundColor(Color.GREEN); 
        } 

        //Colors the selected listview item 
        if (previouslySelectedItem != null) { 
         previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT); 
         Log.v("previouslySelectedItem", "!=null"); 
         Log.v("position", Integer.toString(position)); 
        } 

        view.setBackgroundColor(Color.GREEN); 
        Log.v("position", Integer.toString(position)); 
        Log.v("outofif-view", view.toString()); 
        previouslySelectedItem = view; 
       } 
      }); 

      //Calls async task to get json 
      new GetCarFeatures().execute(); 

      Log.v("return called-Features", "return called"); 
      // Log.v("container-features", container.toString()); 
      return v; 
     } 

ответ

2

Это потому, что RadioButton не вытекает из ViewGroup ..

java.lang.Object

↳ android.view.View

↳ android.widget.TextView

↳ android.widget.Button

↳ android.widget.CompoundButton

↳ android.widget.RadioButton

Вот эти документы, показывающие наследование: http://developer.android.com/reference/android/widget/RadioButton.html

Вместо переходящие в R.id.radioFeatures к fragTransaction.replace вы должны пройти в id макета, который содержит RadioButton.

+0

Как fragTransaction.replace (R.layout.button_tabs, frag); ?? Ожидаемый тип ресурса должен быть одним из id, id - ошибка. – JohnWilliams

+1

Трудно сказать, не видя ваш файл макета, но попробуйте заменить 'R.id.radioFeatures' на' R.id.pager'. – Buddy

+0

Удивительный! Вау. Спасибо!! – JohnWilliams

 Смежные вопросы

  • Нет связанных вопросов^_^