[英]Android Studio can't get button to call
該按鈕在沒有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>
將代碼添加到activity_main.xml文件中的內部按鈕組件中
android:onClick="onClick"
用戶可以按下或單擊按鈕來執行操作!
活動中按鈕的典型用法如下:。
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
,而不是將OnClickListener
應用於活動中的按鈕,而可以將方法分配給XML布局中的按鈕。 您正在嘗試在沒有此屬性的情況下實現這一目標!
現在,當用戶單擊按鈕時,Android系統將調用活動的myClickMethod(View)
(由我創建)方法。 為了使它起作用,該方法必須是公共的並且接受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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.