【问题标题】:Android Studio can't get button to callAndroid Studio 无法获取按钮调用
【发布时间】:2017-06-09 10:19:49
【问题描述】:

该按钮在没有 WebView 的情况下单独工作,但是一旦我添加了只有 WebView 工作并且单击按钮什么都不做。

这是我的 MainActivty.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView) findViewById(R.id.myWebView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://");

}
Intent intentCall;
public void onClick(View v){
    intentCall.setAction("android.intent.action.CALL");
    intentCall.setData(Uri.parse("tel:"));
    startActivity(intentCall);
    finish();
}}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.yellowribbon.evindrake.yellowribbonv3" >
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="org.yellowribbon.evindrake.yellowribbonv3.MainActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/myWebView"
    android:layout_alignParentBottom="true" />

<Button
    android:text="@string/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

【问题讨论】:

  • 发布您的 XML !您是说您的按钮在没有 webView 时进行调用,但是当您添加 webView 时它不起作用?
  • 好的,这是我的 Activity 主目录
  • 你能回答我的问题吗?没有 webView 可以吗?
  • 是的,它不起作用
  • 是的。你没有把你的听众指向你的按钮,这就是原因

标签: javascript java android android-studio


【解决方案1】:

在activity_main.xml文件的内部按钮组件中添加代码

android:onClick="onClick"

【讨论】:

  • 天哪,谢谢你现在工作正常。非常感谢
【解决方案2】:

用户可以按下或单击按钮来执行操作!

活动中按钮的典型用法如下: .

public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.content_layout_id);

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
 }

但是,您可以使用android:onClick 在 XML 布局中为您的按钮分配一个方法,而不是对 Activity 中的按钮应用 OnClickListener你正在尝试在没有这个属性的情况下实现它!

现在,当用户单击按钮时,Android 系统会调用活动的myClickMethod(View)(我创建的)方法。为了使其工作,该方法必须是公共的并接受一个视图作为它的唯一参数。例如:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myClickMethod"
        android:text="button" />

在代码中

public void myClickMethod(View view) {
     // Perform action on click after identify the view
 }

由于Intent intentCall,你也得到了一个空指针

使用您的onClick(View v),如下所示,

public void onClick(View v) {
    String number = "18002738255";
    Uri call = Uri.parse("tel:" + number);
    Intent surf = new Intent(Intent.ACTION_CALL, call);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    startActivity(surf);
}

【讨论】:

  • 应用程序仍然崩溃...我复制了粘贴的代码,将 id 更改为我添加的调用方法,但它仍然崩溃。
  • @Evin Drake 你不需要做任何其他的改变,一旦你指向你的 onClcik 方法,使用我的 onClick 方法并运行它就会起作用
  • 我粘贴了它仍然崩溃
  • 感谢它现在的工作。感谢您的快速回复:D
  • @Evin Drake 如果对您有帮助,您可以将其标记为答案/赞成票:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2017-01-06
  • 2018-08-17
相关资源
最近更新 更多