0

我是新手,而且这是我的第一篇文章,我希望我清楚我的问题,我真的希望有人可以帮助我。

首先,我正在尝试使用一些按钮制作一个简单的页面,其中一个按钮需要将我重定向到网页。但我的问题是我不知道如何制作重定向按钮,所以我看了一下这个:如何在 Android 应用程序中单击按钮时打开网站?

正如我所关注的那样,我真的遇到了很多麻烦。我什至不知道把它们放在哪里。但是在谷歌搜索和阅读 Android 网站之后,我能够做到这一点,耶 :)

无论如何,我仍然有一些小问题,这条线给出了准确的答案。

public class LinkButtons extends Activity {

我将其更改为:

class LinkButtons extends Activity {

我能够去,但不确定这是否是个好主意......

我在 XML 上制作了按钮,但它给了我一个警告(需要硬编码的东西?)我忽略了这个,因为我仍然能够看到带有文本的按钮。

当我启动程序时它工作正常,但是当我触摸此按钮(需要将我重定向到网页)时,程序停止几秒钟,然后退出,并说“进程(我的程序)已停止”

在 Logcat 中我看到了这个:

11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapUtilization:0.25
11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapIdealFree:8388608
11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapConcurrentStart:2097152
11-07 00:22:06.780: D/libEGL(30706): loaded /system/lib/egl/libEGL_adreno200.so
11-07 00:22:06.800: D/libEGL(30706): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-07 00:22:06.810: D/libEGL(30706): loaded /system/lib/egl/libGLESv2_adreno200.so
11-07 00:22:06.820: I/Adreno200-EGL(30706): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)
11-07 00:22:06.820: I/Adreno200-EGL(30706): Build Date: 03/28/13 Thu
11-07 00:22:06.820: I/Adreno200-EGL(30706): Local Branch: 
11-07 00:22:06.820: I/Adreno200-EGL(30706): Remote Branch: m/jb_rel_2.0.3.1
11-07 00:22:06.820: I/Adreno200-EGL(30706): Local Patches: NONE
11-07 00:22:06.820: I/Adreno200-EGL(30706): Reconstruct Branch: NOTHING
11-07 00:22:06.850: D/OpenGLRenderer(30706): Enabling debug mode 0
11-07 00:22:16.850: D/AndroidRuntime(30706): Shutting down VM
11-07 00:22:16.850: W/dalvikvm(30706): threadid=1: thread exiting with uncaught exception (group=0x41eb6438)
11-07 00:22:16.850: E/AndroidRuntime(30706): FATAL EXCEPTION: main
11-07 00:22:16.850: E/AndroidRuntime(30706): java.lang.IllegalStateException: Could not find a method goToSu(View) in the activity class com.example.[need to remove this part, sorry].FullscreenActivity for onClick handler on view class android.widget.Button with id 'button_su'
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$1.onClick(View.java:3667)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View.performClick(View.java:4191)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$PerformClick.run(View.java:17229)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Handler.handleCallback(Handler.java:615)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Looper.loop(Looper.java:137)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.app.ActivityThread.main(ActivityThread.java:4963)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at dalvik.system.NativeStart.main(Native Method)
11-07 00:22:16.850: E/AndroidRuntime(30706): Caused by: java.lang.NoSuchMethodException: goToSu [class android.view.View]
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.Class.getMethod(Class.java:915)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$1.onClick(View.java:3660)
11-07 00:22:16.850: E/AndroidRuntime(30706):    ... 11 more

我现在该怎么办?或者我该如何解决这个问题?

我感谢任何答复。

PS:我已经在我当前的代码中添加了那个 .java 代码,这可以吗?还是我必须制作另一个 .java 文件?

PPS:就像我说的,我是一个非常新手,如果你要讲一个高级故事,请解释...谢谢,

编辑:

这是代码,Java 和 XML;

在java之上;

    package com.example.[removed];
import com.example.[removed].util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

然后

     * The instance of the {@link SystemUiHider} for this activity.
 */
private SystemUiHider mSystemUiHider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
            .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

                class LinkButtons extends Activity {

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

                    public void goToSo (View view) {
                        goToUrl ( "https://stackoverflow.com/");
                    }

                    public void goToSu (View view) {
                        goToUrl ( "http://superuser.com/");
                    }

                    private void goToUrl (String url) {
                        Uri uriUrl = Uri.parse(url);
                        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
                        startActivity(launchBrowser);
                    }
                }

                @Override
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

然后是 XML

            <Button
            android:id="@+id/button_so"
            style="?buttonBarButtonStyle"
            android:layout_width="104dp"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:clickable="true"
            android:cursorVisible="true"
            android:linksClickable="true"
            android:onClick="goToSo"

            android:text="StackOverflow" />

        <Button
            android:id="@+id/button_su"
            style="?buttonBarButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:clickable="true"
            android:onClick="goToSu"
            android:text="SuperUser" >
        </Button>
    </LinearLayout>

</FrameLayout>
4

2 回答 2

0

看起来你onClick()在你的Buttonin xml 上声明了你的,这很好。但是您缺少在android:onClick="goToSu". 在你的Activity.

public void goTuSu(View v)
{
     // your code
}

在您发布的链接中,您显然遵循了 Alain Pannetier 的答案,他在该答案中有这种方法

public void goToSu (View view) {
    goToUrl ( "http://superuser.com/");
}

编辑

同样来自您链接到的答案是这种方法

 private void goToUrl (String url) {
    Uri uriUrl = Uri.parse(url);
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);
}

从 调用goToSu()goToUrl()是一种自定义方法...不是 Android 框架的一部分。

您可以在Button Docs中查看如何onClick()通过 xml 实现

于 2013-11-07T00:04:21.100 回答
0

wtsang02 的回答很好,但有点短。

不要在布局中使用 xml 中的 android:onclick 属性。维护是一场噩梦。

只需在活动的 onCreate 方法中执行此操作。

在 onCreate...

按钮 mButton = (Button) findViewById(R.id.myButton); mButtonsetOnClickListener(new OnClickListener(){

  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub

  }});
于 2013-11-07T00:05:25.043 回答