MainActivity无法转换为Fragment。

huangapple go评论90阅读模式
英文:

MainActivity cannot be cast to Fragment

问题

以下是您提供的内容的翻译:

我正在处理这个项目。当我运行程序时,它会出现一些运行时错误,并且应用程序不会显示在模拟器中。它显示了应用程序停止的消息。我认为在 OnAttach 方法中有一个错误。我尝试了很多方法来解决这个问题,但是没有任何作用。我是 Android 开发的新手,请帮助我解决这个问题。

E/AndroidRuntime: 致命异常: main
    进程: com.example.user.workout, PID: 26059
    java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.user.workout/com.example.user.workout.MainActivity}: android.view.InflateException: 二进制 XML 文件的第 10 行: 在充气类片段时出错
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
        at android.app.ActivityThread.access$800(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5643)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
     原因: android.view.InflateException: 二进制 XML 文件的第 10 行: 在充气类片段时出错
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java:6100)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
        at android.app.ActivityThread.access$800(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5643)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
     原因: java.lang.ClassCastException: com.example.user.workout.MainActivity 无法转换为 com.example.user.workout.WorkoutListFragment$WorkoutListListener
        at com.example.user.workout.WorkoutListFragment.onAttach(WorkoutListFragment.java:53)
        at android.support.v4.app.Fragment.onAttach(Fragment.java:1340)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1659)
        at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1905)
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3715)
        at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
        at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
        at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java:6100)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
        at android.app.ActivityThread.access$800(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5643)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
package com.example.user.workout;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class WorkoutListFragment extends ListFragment {

    static interface WorkoutListListener {
        void itemClicked(long id);
    };

    private WorkoutListListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        String[] names = new String[Workout.workouts.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = Workout.workouts[i].getName();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                inflater.getContext(), android.R.layout.simple_list_item_1, names);

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.listener = (WorkoutListListener) activity;
    }

    @Override

<details>
<summary>英文:</summary>

Im working on this project. When i run the program it have some runtime errors and app does not appears in emulator. it show the message app stopped. I think there is a error in OnAttach method. I tried many ways to solve the issue but nothing works.I&#39;m new to Android  development please help me to solve the  issue.




    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.user.workout, PID: 26059
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.workout/com.example.user.workout.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5643)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
         Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
            at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)
            at android.app.Activity.performCreate(Activity.java:6100)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)&#160;
            at android.app.ActivityThread.access$800(ActivityThread.java:178)&#160;
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)&#160;
            at android.os.Handler.dispatchMessage(Handler.java:111)&#160;
            at android.os.Looper.loop(Looper.java:194)&#160;
            at android.app.ActivityThread.main(ActivityThread.java:5643)&#160;
            at java.lang.reflect.Method.invoke(Native Method)&#160;
            at java.lang.reflect.Method.invoke(Method.java:372)&#160;
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)&#160;
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)&#160;
         Caused by: java.lang.ClassCastException: com.example.user.workout.MainActivity cannot be cast to com.example.user.workout.WorkoutListFragment$WorkoutListListener
            at com.example.user.workout.WorkoutListFragment.onAttach(WorkoutListFragment.java:53)
            at android.support.v4.app.Fragment.onAttach(Fragment.java:1340)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1659)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1905)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3715)
            at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
            at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
            at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)&#160;
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)&#160;
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)&#160;
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)&#160;
            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)&#160;
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)&#160;
            at com.example.user.workout.MainActivity.onCreate(MainActivity.java:14)&#160;
            at android.app.Activity.performCreate(Activity.java:6100)&#160;
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)&#160;
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)&#160;
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)&#160;
            at android.app.ActivityThread.access$800(ActivityThread.java:178)&#160;
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)&#160;
            at android.os.Handler.dispatchMessage(Handler.java:111)&#160;
            at android.os.Looper.loop(Looper.java:194)&#160;
            at android.app.ActivityThread.main(ActivityThread.java:5643)&#160;
            at java.lang.reflect.Method.invoke(Native Method)&#160;
            at java.lang.reflect.Method.invoke(Method.java:372)&#160;
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)&#160;
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)&#160;

---


    package com.example.user.workout;


    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.app.ListFragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;


    public class WorkoutListFragment extends ListFragment {


        static interface WorkoutListListener{

         void  itemClicked(long id);
        };

        private WorkoutListListener listener;



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


            String [] names = new String[Workout.workouts.length];
            for(int i =0; i &lt; names.length;i++)
            {
                names[i] = Workout.workouts[i].getName();
            }

            ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(
              inflater.getContext(), android.R.layout.simple_list_item_1,names);

            setListAdapter(adapter);

            return super.onCreateView(inflater, container, savedInstanceState);
        }


        @Override
        public void onAttach(Activity activity)
        {

            super.onAttach(activity);
           this.listener = (WorkoutListListener)activity;
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id)
        {
            if(listener!=null)
            {
                listener.itemClicked(id);
            }
        }

    }
---


    package com.example.user.workout;

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


        }

        public  void  itemClicked(long id)
        {

            WorkoutDetialFragment detials = new WorkoutDetialFragment();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            detials.setWorkoutId(id);
            ft.replace(R.id.fragment_container,detials);
            ft.addToBackStack(null);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();

        }
    }






</details>


# 答案1
**得分**: 0

您的活动应实现`WorkoutListListener`接口并覆盖其方法`itemClicked()`。

```java
public class MainActivity extends AppCompatActivity implements WorkoutListListener {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }

   @Override
   public void itemClicked(long id){
      // 在此处理您的点击事件
   }

}
英文:

Your activity should implement *WorkoutListListener* interface and override its method itemClicked().

public class MainActivity extends AppCompatActivity implements WorkoutListListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void itemClicked(long id){
// handle your click here 
}
}

huangapple
  • 本文由 发表于 2020年8月31日 23:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63674121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定