簡體   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