繁体   English   中英

在Android中切换到其他活动时,应用停止

[英]App stops when switching to another activity in android

当我单击“开始聚会”按钮时,我希望我的Android应用程序切换到“ activity_before_party.xml”,但是当我单击按钮时,该应用程序停止。 我尝试了很多事情,但没有任何有效的方法。 谢谢您的帮助

这是“ activity_home.xml”(主页)的一部分:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="#226B6E"
    tools:context="com.example.emili.partymate.Home"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">


    <Button
        android:id="@+id/idstartParty"
        android:layout_width="260dp"
        android:layout_height="61dp"
        android:backgroundTint="@color/Start_Party_color"
        android:text="Start Party!"
        android:textColor="#ffffff"
        android:textSize="22sp"
        android:onClick="theClick"
        app:layout_constraintBottom_toTopOf="@+id/viewLogs"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.285" />

这是“ home.java”:

package com.example.emili.partymate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class Home extends AppCompatActivity {
    Button startParty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        startParty = (Button) findViewById(R.id.idstartParty);
    }

    public void theClick(View v){
        Intent i = new Intent(this,BeforeParty.class);
        startActivity(i);
    }
}

这是“ BeforeParty.java”

package com.example.emili.partymate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class BeforeParty extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_before_party);
    }
}

最后,是“ activity_before_party.xml”的一部分(我要切换到的页面):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_before_party"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#226B6E"
    tools:context=".BeforeParty">

请同时发布错误,否则将很难找到问题。 可能您尚未在AndroidManifest注册BeforeParty活动。

应该是这样的:

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

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

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

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

</manifest>

但是,这只是一个猜测。 提供更多详细信息,我们将为您提供帮助。

试试这个,改变你的方法theClick

startParty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(this,BeforeParty.class);
startActivity(i);                    
});

替换为:

public void theClick(View v){
    Intent i = new Intent(this,BeforeParty.class);
    startActivity(i);
}

有了这个:

startParty.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), BeforeParty.class);
if (i.resolveActivity(getPackageManager()) != null) {
                    startActivity(i);
                }            }
        });

试试这个,我希望这对你有用

 startParty.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Home.this, BeforeParty.class));
        }
    });

暂无
暂无

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

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