Android 错误 - 不可转换的类型;无法投射 'android.support.v4.app.FragmentActivity'
Android Error - Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity'
当我清理和编译我的 android 项目时,出现以下我无法解决的错误:"Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity' to '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();
}
}
如果能帮助解决这个问题,我们将不胜感激。
我认为您正在使用支持包中的片段。检查片段的导入 class。您应该导入片段 class "android.app.Fragment" 而不是 "android.support.v4.app.Fragment" (因为您使用的是基本框架 Activity class)。如果您想使用支持片段,那么还可以使用支持包 ("AppCompatActivity") 中的 activity。
当我清理和编译我的 android 项目时,出现以下我无法解决的错误:"Inconvertible types; cannot cast 'android.support.v4.app.FragmentActivity' to '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();
}
}
如果能帮助解决这个问题,我们将不胜感激。
我认为您正在使用支持包中的片段。检查片段的导入 class。您应该导入片段 class "android.app.Fragment" 而不是 "android.support.v4.app.Fragment" (因为您使用的是基本框架 Activity class)。如果您想使用支持片段,那么还可以使用支持包 ("AppCompatActivity") 中的 activity。