[英]Android Studio can't get button to call
The button works when its alone without the WebView but once I add that only the WebView works and clicking the button does nothing. 该按钮在没有WebView的情况下可以单独使用,但是一旦添加后,只有WebView可以使用,并且单击该按钮不会执行任何操作。
Here's my MainActivty.java 这是我的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();
}}
Here's my Manifest 这是我的清单
<?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 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>
将代码添加到activity_main.xml文件中的内部按钮组件中
android:onClick="onClick"
Push-buttons can be pressed, or clicked, by the user to perform an action! 用户可以按下或单击按钮来执行操作!
A typical use of a push-button in an activity would be the following: . 活动中按钮的典型用法如下:。
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
}
});
}
}
However, instead of applying an OnClickListener
to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick
. 但是,您可以使用android:onClick
,而不是将OnClickListener
应用于活动中的按钮,而可以将方法分配给XML布局中的按钮。 You are trying to achieve that without this attribute! 您正在尝试在没有此属性的情况下实现这一目标!
Now, when a user clicks the button, the Android system calls the activity's myClickMethod(View)
(that i created) method. 现在,当用户单击按钮时,Android系统将调用活动的myClickMethod(View)
(由我创建)方法。 In order for this to work, the method must be public and accept a View as its only parameter. 为了使它起作用,该方法必须是公共的并且接受View作为其唯一参数。 For example: 例如:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickMethod"
android:text="button" />
and in code 和在代码中
public void myClickMethod(View view) {
// Perform action on click after identify the view
}
Also you are getting a null pointer because of Intent intentCall
另外,由于Intent intentCall
您还获得了空指针
use your onClick(View v)
like below, 使用您的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.