Android错误 - 不可转换的类型;无法投'android.support.v4.app.FragmentActivity'

时间:2016-06-24 16:33:27

标签: android android-fragments android-fragmentactivity

当我去清理并编译我的android项目时,我收到以下错误,我无法解决:“Inconvertible类型;无法将'android.support.v4.app.FragmentActivity'转换为'com.profile.activity.MainActivity “”。这是给我这个错误的代码行:

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

我认为将以下代码行添加到MainActivity.java会修复错误:

public CPU cpu = new CPU();

我没有解决这个问题。这是我在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
}

这是我的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();
    }
}

非常感谢任何解决此问题的帮助。

1 个答案:

答案 0 :(得分:0)

我认为您正在使用支持包中的Fragment。检查片段类的导入。您应该导入片段类“android.app.Fragment”而不是“android.support.v4.app.Fragment”(因为您使用的是基础框架Activity类)。如果要使用支持片段,请使用支持包中的活动(“AppCompatActivity”)。