简体   繁体   English

按下按钮后android studio中的活动不会切换

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

I've already tried searching the Google on how to switch activities in android studio, even in the official documentation with tutorial. 我已经尝试过搜索Google,以了解如何在android studio中切换活动,甚至在带有教程的官方文档中也是如此。 I did it exactly like it was said but I am still unable to get redirected to another activity after clicking a button. 我完全按照说的做,但是单击按钮后仍然无法重定向到另一个活动。

I've entered an onClick name of the method to the button 我在按钮上输入了方法的onClick名称

it looks like this: https://i.imgur.com/pmsztaL.png (can't post images yet) 它看起来像这样: https : //i.imgur.com/pmsztaL.png (尚不能发布图像)

and this is my MainActivity.class file with said method 这是我所说的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));
    }
}

After pressing the button in a phone, the button does nothing. 按下电话中的按钮后,该按钮不起作用。

This is my 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();
    }
}

How come the button doesn't work, what did I do wrong? 为什么按钮不起作用,我做错了什么?

EDITS 编辑

EDIT: Successfully ran emulator and the buttons did in fact work, the problem lies within my phone, the buttons there don't want to work. 编辑:成功运行了模拟器,并且按钮实际上起作用了,问题出在我的手机内,那里的按钮不想起作用。 Where could be the issue now? 现在问题可能在哪里?

EDIT: XML Layout of MainActivity.class 编辑: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>

EDIT: The problem was with scroll view blocking the clickable button so on my phone the button didn't register any clicks, after removing the scrollview the button works normally. 编辑:问题在于滚动视图阻止了可点击按钮,因此在我的手机上,该按钮没有记录任何点击,在删除滚动视图后,该按钮可以正常工作。

I'm afraid that this button doesn't get any listeners attached. 恐怕此按钮没有附加任何侦听器。

The programatic approach 程序化方法

From the Official Android Documentation 官方的Android文档

I'd suggest you use this in your onCreate callback: 我建议您在onCreate回调中使用它:

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

XML approach XML方法

Please double check the following using this approach 请使用此方法仔细检查以下内容

  1. Your activity is registered in the AndroidManifest.xml 您的活动已在AndroidManifest.xml注册
  2. You're including the android namespace to the XML file in the parent container of the layout file. 您会将android名称空间包含到布局文件的父容器中的XML文件中。 IE: xmlns:android="http://schemas.android.com/apk/res/android" IE: xmlns:android="http://schemas.android.com/apk/res/android"
  3. Make sure you're setting the content view for the activity with setContentView(R.layout.your_layout_file); 确保使用setContentView(R.layout.your_layout_file);设置活动的内容视图setContentView(R.layout.your_layout_file);
  4. Once 2 is satisfied make sure you're using the android namespace. 一旦满足2,请确保您使用的是android名称空间。 IE: android:onClick="yourMethod" IE: android:onClick="yourMethod"
  5. As you're currently doing: Make sure the onClick callbacked method has the View view parameter; 当前操作:确保onClick回调方法具有View view参数; no more, no less 不多不少
  6. Make sure the onClick function is public in the activity. 确保onClick函数在活动中是public的。
  7. And that no other clickable views are covering the view you're registering the onClick listener for :) Including the namespace: xmlns:android="http://schemas.android.com/apk/res/android" 而且没有其他可单击的视图覆盖您正在为其注册onClick侦听器的视图:)包括名称空间: xmlns:android="http://schemas.android.com/apk/res/android"

XML file 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>

The activity 活动

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