私のアンドロイドプロジェクトをクリーンアップしてコンパイルすると、解決できないエラーが発生しています: "変換できないタイプ、キャストできません" android.support.v4.app .FragmentActivity 'を' com.profile.activity.MainActivity 'に変更します。これは、私は、このエラーを与えているコードの行です:Androidエラー - 変換不可能なタイプ。 'android.support.v4.app.FragmentActivity'キャストできません
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();
}
}
この問題を解決する任意の助けいただければ幸いです。
'MainActivity'に' ProfilesFragment'をどのように付け加えることができますか? –
@ user268397あなたのフラグメントはMainActivityには添付されていませんが、FragmentActivityを拡張する別のアクティビティに添付されている可能性があります。このため、このキャストエラーが発生しています。 – comrade