2017-01-24 5 views
2

ボタンはWebViewなしで単独で機能しますが、WebViewのみが機能し、ボタンをクリックすると何もしません。Androidスタジオは電話をかけるためのボタンを取得できません

はここでここで内部のボタンにコードを追加します。私のマニフェスト

<?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> 
+1

があなたのXMLを投稿onClick(View v)、以下のように!あなたはあなたのbutonがwebViewを使わずに電話をかけると言っていますが、webViewを追加するとうまくいきません。 –

+0

さて、私の活動のメイン –

+0

私の質問に答えることができますか? webViewを使用しないと動作しますか? –

答えて

0

ユーザーがプッシュボタンを押すと、アクションが実行されます。

アクティビティでのプッシュボタンの一般的な使用方法は、次のとおりです。 。

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 
      } 
     }); 
    } 
} 

しかし、代わりにあなたの活動のボタンにOnClickListenerを適用するには、を使用して、XMLレイアウトであなたのボタンにメソッドを割り当てることができます。 あなたはこの属性なしで達成しようとしています!ユーザーがボタンをクリックしたときに

さて、AndroidシステムはアクティビティのmyClickMethod(View)(私が作成した)メソッドを呼び出します。これを機能させるには、メソッドをpublicにして、唯一のパラメータとして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 
} 

また、あなたがNULLポインタであるための Intent intentCall

使用を取得しているあなたの

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); 
} 
+0

まだアプリがクラッシュしています...コードをコピーしてコピーしました。私は呼び出しメソッドを追加したもののIDを変更しましたが、それでもクラッシュします。 –

+0

@Evin DrakeあなたがonClcikメソッドを使用してonClickメソッドを使用して動作させると、他の変更を行う必要はありません –

+0

私はそれをペーストしましたが、まだクラッシュします –

0

だ私の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(); 
}} 

ですactivity_main.xmlファイル内のコンポーネント

android:onClick="onClick" 
+0

ああ、神様ありがとうございました。ありがとうございました –

関連する問題