按钮打开另一个活动

时间:2016-12-18 11:13:46

标签: android button

我知道这看起来很简单,我已经尝试过其他线程中提到的所有内容,但由于某些原因它无效。

我只是想让phind.xml上的按钮打开activity_alerts.xml,反之亦然,但由于某种原因,它无法正常工作。我想我错过了一些简单的东西。

phind.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/phind"
    android:background="#ff0000">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Alert Message!"
        android:gravity="center"
        android:textSize="36sp"
        android:textColor="@color/black"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/disable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Disable alert"/>

</LinearLayout>

Phind.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.phinder.phinder.R;

public class Phind extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phind);

        Button button = (Button) findViewById(R.id.disable);
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),ActivityAlerts.class);
                startActivity(i);
            }

        });
    }
}

activity_alerts.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_gray"
        android:orientation="vertical"
        tools:context="com.phinder.phinder.Menu.Alerts">


        <Button
            android:id="@+id/test"
            android:layout_height="wrap_content"
            android:text="test your alert"
            android:height="24dp"
            android:layout_width="match_parent"/>

    </LinearLayout>

ActivityAlerts.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.phinder.phinder.R;

public class ActivityAlerts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alerts);

        Button button = (Button) findViewById(R.id.test);
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),Phind.class);
                startActivity(i);
            }

        });
    }
}

清单:

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

    <uses-permission android:name="android.permission.INTERNET" />

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

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Menu.About"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Menu.Alerts"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name=".Menu.Pin"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name=".Menu.Help"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Phind"
            android:label="@string/app_name" />
        <activity
            android:name=".ActivityAlerts"
            android:label="@string/app_name"/>

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    </application>

</manifest>

我删除了activity_alerts.xml中的一些多余代码,以便更容易找到按钮

Logcat从我打开activity_alerts.xml开始到当我点击应该带我到phind.xml的按钮结束时(之后没有任何反应,所以报告结束):

12-18 15:03:10.704 9207-9207/com.phinder.phinder I/Timeline: Timeline: Activity_launch_request id:com.phinder.phinder time:53000744
12-18 15:03:10.749 9207-9207/com.phinder.phinder D/ViewRootImpl: #3 mView = null
12-18 15:03:10.799 9207-9207/com.phinder.phinder D/SecWifiDisplayUtil: Metadata value : none
12-18 15:03:10.799 9207-9207/com.phinder.phinder D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{e76390 I.E...... R.....ID 0,0-0,0}
12-18 15:03:10.824 9207-9261/com.phinder.phinder D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
12-18 15:03:10.839 9207-9207/com.phinder.phinder W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
12-18 15:03:10.854 9207-9207/com.phinder.phinder D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
12-18 15:03:10.884 9207-9207/com.phinder.phinder I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3371e53 time:53000921
12-18 15:03:10.904 9207-9207/com.phinder.phinder V/ActivityThread: updateVisibility : ActivityRecord{e144243 token=android.os.BinderProxy@62090c2 {com.phinder.phinder/com.phinder.phinder.MainActivity}} show : false
12-18 15:03:13.769 9207-9257/com.phinder.phinder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-18 15:03:13.769 9207-9257/com.phinder.phinder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-18 15:03:14.379 9207-9257/com.phinder.phinder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-18 15:03:14.379 9207-9257/com.phinder.phinder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
12-18 15:03:16.799 9207-9207/com.phinder.phinder D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-18 15:03:16.909 9207-9207/com.phinder.phinder D/ViewRootImpl: ViewPostImeInputStage processPointer 1

1 个答案:

答案 0 :(得分:0)

尝试将您的意图改为:在Phind.java中

Intent i = new Intent(ActivityAlerts.this, Phind.class);
ActivityAlerts.this.startActivity(i);
finish();

也宣告你的活动在清单中:

<activity android:name=".ActivityAlerts" android:label="@string/app_name">
</activity>

在phind.xml中使用

tools:context=".Phind"

并在activity_alerts.xml中使用

tools:context=".ActivityAlerts"