我是新手,加上这是我的第一篇文章 我希望我对自己的问题很清楚,我真的希望有人可以帮助我。
首先,我试图用一些按钮创建一个简单的页面,其中一个按钮需要将我重定向到一个网页。 但我的问题是,我不知道如何制作一个重定向按钮,所以我看看这个:How to open a website when a Button is clicked in Android application?
当我跟着我时,我认真地遇到了很多麻烦。 我甚至不知道把它们放在哪里。 但是在Android网站上进行googleing和阅读后,我能够做到这一点,yay:)
无论如何,我仍有小问题, 这条线给了它确切的。
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文件?
P.P.S:就像我说的,我是一个非常新手,如果你要讲一个高级故事,请解释一下...... 谢谢,编辑:
这是代码,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>
答案 0 :(得分:0)
看起来你在xml的onClick()
上声明了Button
,这很好。但是你错过了在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
中看到如何通过xml实现onClick()
答案 1 :(得分:0)
wtsang02的答案很好,但有点短暂。
不要在xml中使用android:onclick属性作为布局。维持这是一场噩梦。
只需在活动的onCreate方法中执行此操作。
in onCreate ...
按钮mButton =(按钮)findViewById(R.id.myButton); mButtonsetOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}});