片段无法转换为 android.app.Activity(私人案例)

Fragment cannot be cast to android.app.Activity (private case)

美好的一天!请特别帮助我处理我的情况。我已经阅读了有关此问题的解决方案,但我不知道如何将其应用到自己身上。

07-13 18:55:28.339 3850-3850/com.zheil.zodiac E/AndroidRuntime: FATAL EXCEPTION: main Process: com.zheil.zodiac, PID: 3850 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zheil.zodiac/com.zheil.zodiac.FirstFragment}: java.lang.ClassCastException: com.zheil.zodiac.FirstFragment cannot be cast to android.app.Activity at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6221) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.ClassCastException: com.zheil.zodiac.FirstFragment cannot be cast to android.app.Activity at android.app.Instrumentation.newActivity(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2598) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:163)  at android.app.ActivityThread.main(ActivityThread.java:6221)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

片段:

package com.zheil.zodiac;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FirstFragment extends Fragment {
    private TextView textView2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        textView2 = (TextView) view.findViewById(R.id.textView2);
        String textFromActivity = getArguments().getString("1");
        textView2.setText(textFromActivity);

        return view;
    }
}

部分代码主要activity:

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Создадим новый фрагмент
        Fragment fragment = null;
        Class fragmentClass = null;

        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch (id) {
            case R.id.nav_camera:
                // Handle the camera action
                fragmentClass = FirstFragment.class;
                break;
            case R.id.nav_gallery:
                fragmentClass = SecondFragment.class;
                break;
            case R.id.nav_slideshow:

                break;
            case R.id.nav_manage:

                break;
            case R.id.nav_share:

                break;
        }

        try {
            assert fragmentClass != null;
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Вставляем фрагмент, заменяя текущий фрагмент
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
        // Выделяем выбранный пункт меню в шторке
        item.setChecked(true);
        // Выводим выбранный пункт в заголовке
        setTitle(item.getTitle());

        Bundle bundle = new Bundle();
        bundle.putString("1", String.valueOf(getMapLastIntent().get("aries")));
        fragment.setArguments(bundle);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Please add instance in your fragment class

 public static YourFragment newInstance() {
        return new YourFragment();
    }

无需创建 Class 实例和类型转换,只需执行此操作

  @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {          
            Fragment fragment = null;    
            int id = item.getItemId();    
            switch (id) {
                case R.id.nav_camera:                    
                    fragment = new FirstFragment();
                    break;
                case R.id.nav_gallery:
                    fragment = SecondFragment();
                    break;
                case R.id.nav_slideshow:

                    break;
                case R.id.nav_manage:

                    break;
                case R.id.nav_share:

                    break;
            }                                
            FragmentManager fragmentManager = getSupportFragmentManager();
            Bundle bundle = new Bundle();
            bundle.putString("1",String.valueOf(getMapLastIntent().get("aries")));
            fragment.setArguments(bundle);
            fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();       
            item.setChecked(true);        
            setTitle(item.getTitle());

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }