简体   繁体   English

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

[英]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'". 当我清理并编译我的android项目时,出现以下无法解决的错误:“类型不可转换;无法将android.support.v4.app.FragmentActivity强制转换为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: 我认为将以下代码行添加到MainActivity.java可以解决该错误:

public CPU cpu = new CPU();

I didn't resolve the issue though. 我没有解决问题。 Here is my code under ProfilesFragment.java: 这是我在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: 这是我的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. 我认为您正在使用Support包中的Fragment。 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). 您应该导入片段类“ android.app.Fragment”,而不是“ android.support.v4.app.Fragment”(因为您正在使用基本框架Activity类)。 If you want to use the support fragment then use also the activity from the support package ("AppCompatActivity"). 如果要使用支持片段,请同时使用支持包中的活动(“ AppCompatActivity”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将clcik上的android.app.Application强制转换为android.support.v4.app.FragmentActivity - android.app.Application cannot be cast to android.support.v4.app.FragmentActivity on clcik 无法在ViewModelProviders中解析android.support.v4.app.FragmentActivity的方法 - Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders 无法将ContraceptiveFragment中的ContraceptiveFragment()应用于(android.support.v4.app.FragmentActivity) - ContraceptiveFragment( ) in ContraceptiveFragment cannot be applied to (android.support.v4.app.FragmentActivity) 无法解析构造函数&#39;GridLayoutManager(android.support.v4.app.FragmentActivity) - Cannot resolve constructor 'GridLayoutManager(android.support.v4.app.FragmentActivity) “无法找到类'android.support.v4.app.FragmentActivity'”错误 - “Could not find class 'android.support.v4.app.FragmentActivity'” Error android.support.v4.app.FragmentActivity扩展了什么? - What does android.support.v4.app.FragmentActivity extend? android.support.v4.app.FragmentActivity无法解析 - android.support.v4.app.FragmentActivity can not be resolved android.support.v4.app.FragmentActivity缺少操作栏 - android.support.v4.app.FragmentActivity missing action bar “TAG”在“android.support.v4.app.FragmentActivity”中有私有访问权限 - 'TAG' has private access in 'android.support.v4.app.FragmentActivity' 迁移到androidx时无法访问类android.support.v4.app.FragmentActivity - cannot access class android.support.v4.app.FragmentActivity when i migrate to androidx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM