0

我正在尝试单击按钮打开 Web 浏览器窗口。

我的点击事件似乎没有被调用。LogCat 没有显示任何错误或任何被调用的证据。我有一种感觉,这是因为我有 2 个名为 ' onClick' 的方法,但删除第二种方法会导致错误。我可以通过使 MainActivity 抽象来修复此错误,但这会使应用程序崩溃。

我觉得这是一个简单的解决方法,但是在翻阅文档和几个教程之后,我找不到答案。

代码如下,后面是我的清单。提前致谢。

package com.spotsofmagic.spotsofmagic;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {
    private static final String TAG = "Activity...";
    private NfcAdapter mAdapter;
    private TextView mTextView;

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

        // grab our NFC Adapter
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // TextView that we'll use to output messages to screen
        mTextView = (TextView)findViewById(R.id.text_view);

        //displayMessage("Loading payload...");

    }

    private void displayMessage(String message) {
        mTextView.setText(message);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        // do something when the button is clicked
        displayMessage("Loading broswer");
        Uri uriUrl = Uri.parse("http://www.***.com/");      
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
        startActivity(launchBrowser);  

        if(v.getId() == R.id.btnVisitWebsite) {

        }
    }

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }
}

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spotsofmagic.spotsofmagic"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="auto">

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />

    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         <activity
            android:name=".CardActivity"
            android:label="@string/app_name" >

            <!-- Handle a collectable card NDEF record -->
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <data android:mimeType="application/vnd.spotsofmagic.spotsofmagic"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
         </activity>

    </application>

</manifest>
4

3 回答 3

3

因为你没有关联点击事件。

如果要将其绑定到按钮(或)某物,则需要为“this”绑定它

例子:

   button.setOnClickListener(this);
于 2012-09-06T14:00:56.800 回答
3

你有一个,OnClickListener但你没有把它附加到任何按钮上。尝试

((Button) findViewById(R.id.your_button_id)).setOnClickListener(this);

编辑:另一个问题是,您实施DialogInterface.OnClickListener而不是View.OnClickListener在您的Activity.

删除导入

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

并添加

import android.view.View.OnClickListener;

您还可以删除

public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

}

在这之后。

于 2012-09-06T14:02:11.910 回答
1

为避免混淆,如果您不以编程方式创建视图,也可以使用 XML 来完成。你可以做这样的事情。

<Button android:width="20dp" android:height="20dp" android:onClick="openBrowser" />

并在您的程序中提供方法

public void openBrowser(View v)
{
   /*  do your stuff here */

 }
于 2012-09-06T14:26:14.413 回答