Android Studio错误 - Inconvertible类型,无法转换Fragment

时间:2016-08-08 14:16:42

标签: java android

enter image description here尝试使用片段制作一个简单的程序。我得到的确切错误是:

"Inconvertible types, Cannot cast android.app.Fragment to com.example.something.something.WorkoutDetailFragment"

代码:

public class MainActivity extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag); //ERROR!
         frag.setWorkoutid(1);
     }

}

(我的activity_main_xml代码)

1 个答案:

答案 0 :(得分:1)

你的WorkoutDetailFragment应该扩展android.app.Fragment,而不是android.support.v4.app.Fragment。试试吧〜