简体   繁体   English

意图启动活动时应用崩溃

[英]App Crashing When Intent to Launch Activity Called

I am a beginner using Android Studio to Create an application. 我是使用Android Studio创建应用程序的初学者。

On my homepage/ActivityMain I wish to put a textView that when clicked, will launch into another RelativeLayout activity. 我希望在我的主页/ ActivityMain上放置一个textView,单击该文本视图将启动另一个RelativeLayout活动。

I have attempted to do this using an intent, but the application crashes when the textbox is clicked. 我试图使用一种意图执行此操作,但是单击文本框时应用程序崩溃。 Please note that for all code, statements which I believe to be irrelevant were deleted (import, bundle) Below is my code for activity_main.xml: 请注意,对于所有代码,我认为无关的语句都被删除(导入,捆绑)。以下是我的activity_main.xml代码:

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:layout_margin="16dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Organelles" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/membrane"
    android:text="Cell Membrane"
    android:onClick="explainCellMembrane" />

Below is my code for MainActivity.java containing the code containing my intent: 以下是我的MainActivity.java代码,其中包含包含我的意图的代码:

public class MainActivity extends AppCompatActivity {

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

        public void explainCellMembrane(View view) {
            Intent myIntent = new Intent(MainActivity.this, MembraneActivity.class);
            MainActivity.this.startActivity(myIntent);
        }
}

Below is the code for the Class(MembraneActivity) launched by the intent: 以下是意图启动的Class(MembraneActivity)的代码:

    package com.example.android.cellularbiology;

public class MembraneActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_membrane);
    }

}

Finally is my code for the layout of my custom Java Class: 最后是我的自定义Java类的布局代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Membrane Explanation Page"/>

</LinearLayout>

In AndroidStudio there are no errors (red underline) When the app crashes it does not show error, just says that it has stopped. 在AndroidStudio中没有错误(红色下划线),当应用程序崩溃时,它并不表示错误,只是说它已停止。

How do I fix my intent? 如何解决我的意图? Or should another method be used? 还是应该使用其他方法?

Thanks for your help. 谢谢你的帮助。

Android Manifest: Android清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.cellularbiology">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Logs: 日志:

    01/03 00:06:12: Launching app
No apk changes detected since last installation, skipping installation of /Users/FideronTSANG/Desktop/CellularBiology/app/build/outputs/apk/app-debug.apk
$ adb shell am force-stop com.example.android.cellularbiology
$ adb shell am start -n "com.example.android.cellularbiology/com.example.android.cellularbiology.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 24150 on device samsung-sm_c9000-b9dec0e1
Application terminated.

Conclusion: Thankyou everyone for answering and helping out a noob! 结论:谢谢大家回答和帮助菜鸟! You completely saved my project! 您完全保存了我的项目! I hope you have a nice day :) 我希望你有个美好的一天 :)

The intent worked after I added 我添加后意图就起作用了

<activity
    android:name = ".MembraneActivity">
</activity>

To my android manifest. 以我的android清单。

I also used the improved intent provided by 我还使用了由

public void explainCellMembrane(View view) {

    startActivity(new Intent(MainActivity.this, MembraneActivity.class));

}

First of all replace your explainCellMembrane method with this: 首先 ,用以下代码替换您的explainCellMembrane方法:

public void explainCellMembrane(View view) {

        startActivity(new Intent(MainActivity.this, MembraneActivity.class));

    }

Second, Try Cleaning and Rebuilding your project and then share your logCat in the question. 其次,尝试清理和重建项目,然后在问题中共享logCat

EDIT: After checking your logCat I can see the error is not in your logCat but it is in the device so try deleting the application in your emulator or mobile device and then install it again through Android-Studio, this works for me too as it often occurs randomly. 编辑:检查您的logCat我可以看到错误不在您的logCat但它在设备中,因此请尝试在模拟器或移动设备中删除该应用程序,然后通过Android-Studio重新安装它,这也对我logCat常随机发生。

You need to add the class to your manifest. 您需要将类添加到清单中。

Add the following... 添加以下内容...

<activity android:name=".MembraneActivity"></activity>

This will go under the </activity> tag. 这将放在</activity>标记下。

I think, there is no problem with your Intent . 我认为,您的Intent没问题。 And the problem is you are missing to add your MembraneActivity in the Manifest file, that will go under <activity><activity/> tag. 问题是您缺少在Manifest文件中添加MembraneActivity ,该文件将放在<activity><activity/>标记下。 For example, 例如,

<activity
    android:name = ".MembraneActivity">
</activity>

Whenever you add a new Activity in your app, you must have to add the Activity name in the Manifest file. 每当您在应用程序中添加新的Activity时,都必须在Manifest文件中添加Activity名称。 Otherwise you app will not recognize the Activity you are trying to start through the Intent myIntent . 否则,您的应用程序将无法识别要通过Intent myIntent启动的Activity

Now after adding the MembraneActivity in the Manifest file, it will be like this, 现在,在Manifest文件中添加MembraneActivity之后,就像这样,

<?xml version="1.0" encoding="utf-8"?>
    <manifest  
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.cellularbiology">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" 
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <activity 
                android:name=".MainActivity">

                <intent-filter>
                    <action 
                        android:name="android.intent.action.MAIN" />
                    <category
                        android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

            </activity>

            <!--your missing activity added here-->
            <activity
                android:name = ".MembraneActivity">
            </activity>

        </application>
    </manifest>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM