致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo-为什么这样?

时间:2019-07-16 16:56:20

标签: android

我无法在手机上打开应用程序,它也显示了一些线程错误。

当我在模拟器中运行时,应用程序崩溃并显示“不幸的是,应用程序已停止”。并显示错误AndroidRuntime:FATAL EXCEPTION:main java.lang.RuntimeException:无法在我的logcat中启动活动ComponentInfo。由于我是Java和android的新手,请帮助我解决此错误。

MainActivity.java

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentTransaction;

    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.toolbar);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            Fragment fragment = new Fragment();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.content_frame, fragment);
            ft.commit();

        }
    }

运行时显示错误

        E/AndroidRuntime: FATAL EXCEPTION: main
            Process: com.example.catchat, PID: 4709
            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.catchat/com.example.catchat.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080065 (com.example.catchat:id/content_frame) for fragment Fragment{9f3a5e4 (3a9b06d6-7ab2-4c41-9e06-78df8f2f74dc) id=0x7f080065}
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3062)
                at android.app.ActivityThread.-wrap11(Unknown Source:0)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1756)
                at android.os.Handler.dispatchMessage(Handler.java:106)
                at android.os.Looper.loop(Looper.java:192)
                at android.app.ActivityThread.main(ActivityThread.java:6806)
                at java.lang.reflect.Method.invoke(Native Method)
                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
             Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080065 (com.example.catchat:id/content_frame) for fragment Fragment{9f3a5e4 (3a9b06d6-7ab2-4c41-9e06-78df8f2f74dc) id=0x7f080065}
                at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:875)
                at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
                at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
                at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
                at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
                at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
                at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)
                at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
                at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2660)
                at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2610)
                at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
                at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
                at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
                at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339)
                at android.app.Activity.performStart(Activity.java:7161)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3062) 
                at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1756) 
                at android.os.Handler.dispatchMessage(Handler.java:106) 
                at android.os.Looper.loop(Looper.java:192) 
                at android.app.ActivityThread.main(ActivityThread.java:6806) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875) 
        I/Process: Sending signal. PID: 4709 SIG: 9
        Application terminated.

1 个答案:

答案 0 :(得分:0)

您已经使用Fragment类创建了没有任何视图实现的新实例。

您必须扩展Fragment类,然后才能为其提供视图。然后,您就可以使用它了。

class MyFrag extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_layout,container,false)
    }
}

然后使用它:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        MyFragfragment = new MyFrag();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.content_frame, fragment);
        ft.commit();

    }
}

frag_layout.xml采用 res>布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

activity_main.xml :添加content_frame这样的ID:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="+id/content_frame"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>