簡體   English   中英

按下按鈕后android studio中的活動不會切換

[英]The activities in android studio don't switch after pressing the button

我已經嘗試過搜索Google,以了解如何在android studio中切換活動,甚至在帶有教程的官方文檔中也是如此。 我完全按照說的做,但是單擊按鈕后仍然無法重定向到另一個活動。

我在按鈕上輸入了方法的onClick名稱

它看起來像這樣: https : //i.imgur.com/pmsztaL.png (尚不能發布圖像)

這是我所說的MainActivity.class文件

public class MainActivity extends AppCompatActivity {

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

    public void handleButtonAddNew(View view) {
        MainActivity.this.startActivity(new Intent(MainActivity.this, AddItemActivity.class));
    }
}

按下電話中的按鈕后,該按鈕不起作用。

這是我的AddItemActivity.class

public class AddItemActivity extends AppCompatActivity {

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


    public void handleButtonRemember(View view) {
        finish();
    }
}

為什么按鈕不起作用,我做錯了什么?

編輯

編輯:成功運行了模擬器,並且按鈕實際上起作用了,問題出在我的手機內,那里的按鈕不想起作用。 現在問題可能在哪里?

編輯:MainActivity.class的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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/buttonAddNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="188dp"
        android:layout_marginRight="188dp"
        android:layout_marginBottom="264dp"
        android:text="@string/buttonAdd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="395dp"
        android:layout_height="715dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

</android.support.constraint.ConstraintLayout>

編輯:問題在於滾動視圖阻止了可點擊按鈕,因此在我的手機上,該按鈕沒有記錄任何點擊,在刪除滾動視圖后,該按鈕可以正常工作。

恐怕此按鈕沒有附加任何偵聽器。

程序化方法

官方的Android文檔

我建議您在onCreate回調中使用它:

Button button = (Button) findViewById(R.id.your_btn_id);
button.setOnClickListener((view) -> { theMethodYouWantToCallHere(view); });

XML方法

請使用此方法仔細檢查以下內容

  1. 您的活動已在AndroidManifest.xml注冊
  2. 您會將android名稱空間包含到布局文件的父容器中的XML文件中。 IE: xmlns:android="http://schemas.android.com/apk/res/android"
  3. 確保使用setContentView(R.layout.your_layout_file);設置活動的內容視圖setContentView(R.layout.your_layout_file);
  4. 一旦滿足2,請確保您使用的是android名稱空間。 IE: android:onClick="yourMethod"
  5. 當前操作:確保onClick回調方法具有View view參數; 不多不少
  6. 確保onClick函數在活動中是public的。
  7. 而且沒有其他可單擊的視圖覆蓋您正在為其注冊onClick偵聽器的視圖:)包括名稱空間: xmlns:android="http://schemas.android.com/apk/res/android"

XML文件

<FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <Button android:text="Click me!"  android:onClick="clicked" android:layout_height="wrap_content" android:layout_width="wrap_content"/>

</FrameLayout>

活動

public class MainActivity extends AppCompatActivity {

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

    public void clicked(View view){
        Toast.makeText(this, "Hey! Im clicked!",Toast.LENGTH_SHORT).show();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM