Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 方法在启动时未调用_Android_Android Studio - Fatal编程技术网

Android 方法在启动时未调用

Android 方法在启动时未调用,android,android-studio,Android,Android Studio,所以我对android的开发还是个新手 我阅读参考资料时希望有一个可点击的按钮,并在xml中进行一些更改。我遇到的问题是,每当我试图点击它时,什么都没有发生。我的手机和android studio上都没有错误消息。我想知道遗漏了什么,或者有什么我不知道的,我应该知道的 这是我的项目中的java public class test_button extends Activity { Button button ; @Override public void onCreate(Bundle sav

所以我对android的开发还是个新手

我阅读参考资料时希望有一个可点击的按钮,并在xml中进行一些更改。我遇到的问题是,每当我试图点击它时,什么都没有发生。我的手机和android studio上都没有错误消息。我想知道遗漏了什么,或者有什么我不知道的,我应该知道的

这是我的项目中的java

public class test_button extends Activity {
Button button ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_button);
    click();
}
public void click(){
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

                        Intent browserIntent =
                                new Intent(Intent.ACTION_VIEW, Uri.parse("http://reddit.com"));
                        startActivity(browserIntent);
        }
    });
}
这是我的xml

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<include layout="@layout/layout_toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:id="@+id/toolbar"/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/profile_page_activity_body"
    android:layout_below="@id/toolbar"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="60sp"
        android:text="Do things"
        android:textColor="#ffffff"
        android:textSize="40sp"
        android:id="@+id/button"
        android:textAllCaps="false"
        android:background="@drawable/btn_greenbox"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp" />


</RelativeLayout>

Edit1:在onClick()上,我确实尝试了Toast,但结果类似,什么也没发生


Edit2:每当我点击按钮时,就会出现一个点击动画,类似这样的调用意图可能会在您的链接中出现问题:

点击进入:-

**Intent i = new Intent(Intent.ACTION_VIEW, 
           Uri.parse("https://www.reddit.com/"));
    startActivity(i);**

只需放置一个而不是

将click listener设置为:

public class TestButton extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        // Intent code here;
    }
}

onCreate
中设置onclick listener而不是该方法,然后重试

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_button);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

                        Intent browserIntent =
                                new Intent(Intent.ACTION_VIEW, Uri.parse("http://reddit.com"));
                        startActivity(browserIntent);
        }
    });
}

而且,
test_按钮
对于类来说不是一个好名字。将其更改为
TestButton
。(使用重构[shift+f6]重命名,这样就不会出现引用错误)

如何运行此操作?在模拟器中?在您的设备上?不确定,但请更改android:id=“@+id/button”,在这种情况下,不要将id设置为button,而是使用其他类似btn_do_的东西或您明智地给予的东西,但不要使用button。在.java中,也就是说,您的活动类按钮很好。你能告诉我你的OnClickListener的确切输入行吗?@JoshuaCarmoty在我的设备上。我在两台设备上试过,两台都有相似的结果。你能告诉我你的OnClickListener的确切导入行吗?@RajenRaiyarela对不起,“确切导入行”是什么意思?“导入android.view.view.OnClickListener”?将其更改为https仍然没有任何作用。在上面的链接中,简单地从编辑文本获取文本。在您的情况下,您可以直接将URL传递给intent。希望对你有用。