2 jason xcj Jason_xcj 于 2015.07.04 20:51 提问

FragmentActivity强制转换为Activity

public class BaseFragment extends Fragment {

protected MainActivity mainActivity;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mainActivity = (MainActivity)getActivity();
}

protected  void intent2Activity(Class<? extends Activity> artActivity){

    Intent intent = new Intent(mainActivity,artActivity);
    startActivity(intent);
}

}

mainActivity = (MainActivity)getActivity();
报错:Cannot cast from FragmentActivity to MainActivity

2个回答

bdmh
bdmh   Ds   Rxr 2015.07.04 20:53

(Activity)xxxxx,这样就可以了

strutce
strutce   Ds   Rxr 2015.07.04 21:48
Csdn user default icon
上传中...
上传图片
插入图片