当我点击按钮进入其他活动时,应用程序在android studio上关闭?

问题描述 投票:-1回答:1

我检查了所有的东西,我确信我的代码在活动方面是正确的,但我不能找出错误,我试着检查AndroidManifest.xml,但我没有看到这个问题。当我退出模拟器时,我得到这个错误。

 *

> V/FA: onActivityCreated D/AndroidRuntime: Shutting down VM
> E/AndroidRuntime: FATAL EXCEPTION: main
>     Process: com.example.dictionnary, PID: 7465
>     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dictionnary/com.example.dictionnary.PlannerActivity}:
> android.view.InflateException: Binary XML file line #72 in
> com.example.dictionnary:layout/activity_planner: Binary XML file line
> #72 in com.example.dictionnary:layout/activity_planner: Error inflating class android.support.widget.RecyclerView
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
>         at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
>         at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
>         at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
>         at android.os.Handler.dispatchMessage(Handler.java:107)
>         at android.os.Looper.loop(Looper.java:214)
>         at android.app.ActivityThread.main(ActivityThread.java:7356)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
>      Caused by: android.view.InflateException: Binary XML file line #72 in com.example.dictionnary:layout/activity_planner: Binary XML file line #72 in com.example.dictionnary:layout/activity_planner:
> Error inflating class android.support.widget.RecyclerView
>      Caused by: android.view.InflateException: Binary XML file line #72 in com.example.dictionnary:layout/activity_planner: Error inflating class android.support.widget.RecyclerView
>      Caused by: java.lang.ClassNotFoundException: android.support.widget.RecyclerView
>         at java.lang.Class.classForName(Native Method)
>         at java.lang.Class.forName(Class.java:454)
>         at android.view.LayoutInflater.createView(LayoutInflater.java:815)
>         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
>         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
>         at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
>         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
>         at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
>         at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
>         at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
>         at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
>         at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
>         at com.example.dictionnary.PlannerActivity.onCreate(PlannerActivity.java:36)
>         at android.app.Activity.performCreate(Activity.java:7802)
>         at android.app.Activity.performCreate(Activity.java:7791)
>         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
>         at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
>         at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
>         at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
>         at android.os.Handler.dispatchMessage(Handler.java:107)
>         at android.os.Looper.loop(Looper.java:214)
>         at android.app.ActivityThread.main(ActivityThread.java:7356)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
> E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't
> find class "android.support.widget.RecyclerView" on path:
> DexPathList[[zip file
> "/data/app/com.example.dictionnary-NMLUdW6Y7szWNvSxehbDwQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.dictionnary-NMLUdW6Y7szWNvSxehbDwQ==/lib/x86,
> /system/lib, /system/product/lib]]
>         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
>           ... 28 more

*

这是我的简单的MainActivity

       public class MainActivity extends AppCompatActivity {
        private Button searchButton, plannerButton;

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

            searchButton = (Button) findViewById (R.id.main_search);
            plannerButton = (Button) findViewById (R.id.main_planner);
            searchButton.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick(View v) {
                    openActivity2();

                }
            });
        }
        public void openActivity2(){
            Intent intent = new Intent (this, PlannerActivity.class);
            startActivity(intent);

        }
    }

    And here my activity_main.xml:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/welcome"
        tools:context=".MainActivity">

        <ImageView
            android:id="@+id/app_logo"
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:src="@drawable/applogo"
            />

        <TextView
            android:id="@+id/app_slogan"
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="88dp"
            android:layout_marginTop="140dp"
            android:text="Discover Apiales"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textSize="22dp"
            android:textStyle="bold|italic"
            />
        <Button
            android:id="@+id/main_planner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/buttons"

            android:padding="20dp"
            android:textAllCaps="false"
            android:textSize="10sp"
            android:text="Planner"
            android:textColor="@android:color/white"

            />
        <Button
            android:id="@+id/main_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/main_planner"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/input_design"

            android:padding="20dp"
            android:textAllCaps="false"
            android:textSize="10sp"
            android:text="Recherche "
            android:textColor="@android:color/white"

            />




    </RelativeLayout>


and here is the second Activity I want to go to once I click on search button, then I'll do the same with planner button to go to an other activity

        public class PlannerActivity extends AppCompatActivity {

        TextView titlepage, subtitlepage, endpage;
        Button btnAddNew;

        DatabaseReference reference;
        RecyclerView ourdoes;
        ArrayList<MyDoes> list;
        DoesAdapter doesAdapter;

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

            titlepage = findViewById(R.id.titlepage);
            subtitlepage = findViewById(R.id.subtitlepage);
            endpage = findViewById(R.id.endpage);

            btnAddNew = findViewById(R.id.btnAddNew);

            // import font
            Typeface MLight = Typeface.createFromAsset(getAssets(), "fonts/ML.ttf");
            Typeface MMedium = Typeface.createFromAsset(getAssets(), "fonts/MM.ttf");

            // customize font
            titlepage.setTypeface(MMedium);
            subtitlepage.setTypeface(MLight);
            endpage.setTypeface(MLight);

            btnAddNew.setTypeface(MLight);

            btnAddNew.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent a = new Intent(PlannerActivity.this,NewTaskAct.class);
                    startActivity(a);
                }
            });


            // working with data
            ourdoes = findViewById(R.id.ourdoes);
            ourdoes.setLayoutManager(new LinearLayoutManager (this));
            list = new ArrayList<MyDoes>();

            // get data from firebase
            reference = FirebaseDatabase.getInstance().getReference().child("DoesApp");
            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // set code to retrive data and replace layout
                    for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                    {
                        MyDoes p = dataSnapshot1.getValue(MyDoes.class);
                        list.add(p);
                    }
                    doesAdapter = new DoesAdapter(PlannerActivity.this, list);
                    ourdoes.setAdapter(doesAdapter);
                    doesAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    // set code to show an error
                    Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
                }
            });

        }
    }


Here is my activity_planner.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F4F4F4"
        tools:context=".MainActivity">


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:background="@color/colorPrimaryDark"
            android:layout_height="198dp">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_marginLeft="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/titlepage"
                        android:layout_marginTop="20dp"
                        android:textSize="32sp"
                        android:textColor="#FFF"
                        android:text="My Does"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/subtitlepage"
                        android:textSize="18sp"
                        android:textColor="#4A4E6A"
                        android:layout_marginTop="8dp"
                        android:text="Finish Them Quickly Today"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <Button
                    android:layout_marginTop="35dp"
                    android:layout_marginLeft="60dp"
                    android:id="@+id/btnAddNew"
                    android:text="+"
                    android:background="@drawable/bgbtnnew"
                    android:textSize="38sp"
                    android:textColor="#FFF"
                    android:textAlignment="center"
                    android:layout_width="52dp"
                    android:layout_height="52dp" />

            </LinearLayout>

            <View
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:background="#131E69"
                android:layout_height="1dp" />

        </LinearLayout>

        <android.support.widget.RecyclerView
            android:id="@+id/ourdoes"
            android:layout_width="match_parent"
            android:layout_marginTop="-60dp"
            android:layout_marginRight="16dp"
            android:layout_marginLeft="16dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/endpage"
            android:textSize="16sp"
            android:textColor="#9A9A9A"
            android:layout_marginTop="20dp"
            android:text="No More Does"
            android:textAlignment="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>



error when the app crashes and quit to go back to the menu of the emulator

VFA:连接到远程服务VFA:活动暂停,时间:1117469DAndroidRuntime: 关闭VMEAndroidRuntime: FATAL EXCEPTION: main Process: com.example.dictionnary, PID: 11242 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dictionnarycom.example.dictionnary.PlannerActivity}: java.lang.RuntimeException: Font asset not found fontsML.ttf at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction. LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor. java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread. 在java.lang.reflect.Method.invoke(Native Method)处的java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 原因是:java.lang.RuntimeException: Font asset not found fontsML.ttf at android.graphics.Typeface.createFromAsset(Typeface.java:965) at com.example.dictionnary.PlannerActivity.onCreate(PlannerActivity.java:45) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app. Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.executeLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android. app.servertransaction.TransactionExecutor.executor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android. os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com. android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) IProcess: 发送信号。PID: 11242 SIG: 9

xml android-studio android-intent onclicklistener avd-manager
1个回答
0
投票

你使用的是旧版本的Recyclerview,可能还没有完全迁移到AndroidX上,只需替换为

android.support.widget.RecyclerView

在你的xml中加上这个

androidx.recyclerview.widget.RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F4F4F4"
    tools:context=".MainActivity">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:background="@color/colorPrimaryDark"
        android:layout_height="198dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_marginLeft="16dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/titlepage"
                    android:layout_marginTop="20dp"
                    android:textSize="32sp"
                    android:textColor="#FFF"
                    android:text="My Does"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/subtitlepage"
                    android:textSize="18sp"
                    android:textColor="#4A4E6A"
                    android:layout_marginTop="8dp"
                    android:text="Finish Them Quickly Today"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <Button
                android:layout_marginTop="35dp"
                android:layout_marginLeft="60dp"
                android:id="@+id/btnAddNew"
                android:text="+"
                android:background="@drawable/bgbtnnew"
                android:textSize="38sp"
                android:textColor="#FFF"
                android:textAlignment="center"
                android:layout_width="52dp"
                android:layout_height="52dp" />

        </LinearLayout>

        <View
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:background="#131E69"
            android:layout_height="1dp" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ourdoes"
        android:layout_width="match_parent"
        android:layout_marginTop="-60dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/endpage"
        android:textSize="16sp"
        android:textColor="#9A9A9A"
        android:layout_marginTop="20dp"
        android:text="No More Does"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

0
投票

你可以检查你的activity_planner.xml文件。检查第72行android.support.widget.RecyclerView。

© www.soinside.com 2019 - 2024. All rights reserved.