简体   繁体   中英

Android Error - Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity'

When I go to clean and compile my android project I am getting the following error that I cannot resolve: "Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity' to 'com.profile.activity.MainActivity'". This is the line of code that is giving me this error:

this.cpu = ((MainActivity) getActivity()).cpu

I thought adding the following line of code to the MainActivity.java would fix the error:

public CPU cpu = new CPU();

I didn't resolve the issue though. Here is my code under ProfilesFragment.java:

public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.cpu = ((MainActivity) getActivity()).cpu;  //This is the line of code giving me the error
}

Here is my code for MainActivity.java:

public class MainActivity extends Activity {
public CPU cpu = new CPU();
private String CANCEL;
private String DELETE;
private String EDIT;
private DatabaseAdapter dbHelper;
private ListView scheduleCustomListView;
public ArrayList<Schedule> scheduleList = new ArrayList();

public ArrayList<Schedule> getScheduleList() {
    this.scheduleList = this.dbHelper.retrieveAllSchedules();
    return this.scheduleList;
}

public void onCreate(Bundle savedInstanceState) {
    this.dbHelper = new DatabaseAdapter(this);
    this.dbHelper.open();
    this.DELETE = getText(R.string.deleteSchedule).toString();
    this.EDIT = getText(R.string.editSchedule).toString();
    this.CANCEL = getText(R.string.cancel).toString();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.scheduleCustomListView = (ListView) findViewById(R.id.scheduleList);
    this.scheduleCustomListView.setAdapter(new ListViewAdapter(this, R.layout.row, getScheduleList()));
    this.scheduleCustomListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2, long arg3) {
            Intent intentActivityUpdateSchedule = new Intent(MainActivity.this, UpdateScheduleActivity.class);
            intentActivityUpdateSchedule.putExtra("schedule", (Serializable) MainActivity.this.scheduleList.get(arg2));
            MainActivity.this.startActivity(intentActivityUpdateSchedule);
        }
    });
    registerForContextMenu(this.scheduleCustomListView);
    ((LinearLayout) findViewById(R.id.bopenlayoutaddschedule)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            MainActivity.this.startActivity(new Intent(MainActivity.this, AddNewScheduleActivity.class));
        }
    });
}

protected void onRestart() {
    super.onRestart();
    this.scheduleCustomListView.setAdapter(new ListViewAdapter(this, R.layout.row, getScheduleList()));
}

protected void onResume() {
    super.onResume();
}

protected void onPause() {
    super.onPause();
}

protected void onDestroy() {
    super.onDestroy();
    if (this.dbHelper != null) {
        this.dbHelper.close();
    }
}

Any help resolving this issue would be greatly appreciated.

I think you are using the Fragment from the Support package. Check the imports of your fragment class. You should import the fragment class "android.app.Fragment" and not the "android.support.v4.app.Fragment" (because you are using the base framework Activity class). If you want to use the support fragment then use also the activity from the support package ("AppCompatActivity").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM