2012-05-02 13 views
1

カスタムダイアログの中でwebviewを表示しようとしています。そのダイアログの外で動作しています。下のコードは、カスタムダイアログのXMLと一緒にxmlです。アンドロイドのカスタムダイアログ内でwebviewを表示するにはどうすればいいですか?

public class TellaFriend extends Activity { 

static final int CUSTOM_DIALOG_ID = 0; 
TextView customDialog_TextView; 
EditText customDialog_EditText; 
Button customDialog_Update, customDialog_Dismiss;; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button buttonStartDialog = (Button) findViewById(R.id.button1); 
    buttonStartDialog.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      showDialog(CUSTOM_DIALOG_ID); 
     } 
    }); 
} 

private Button.OnClickListener customDialog_UpdateOnClickListener = new Button.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     String sharedFact = customDialog_EditText.getText().toString(); 
    } 
}; 

private Button.OnClickListener customDialog_DismissOnClickListener = new Button.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     dismissDialog(CUSTOM_DIALOG_ID); 
    } 
}; 

@Override 
protected Dialog onCreateDialog(int id) { 
    // TODO Auto-generated method stub 
    Dialog dialog = null; 
    switch (id) { 
    case CUSTOM_DIALOG_ID: 

     dialog = new Dialog(this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setCancelable(false); 
     dialog.setContentView(R.layout.customlayout); 

     WebView webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.setBackgroundColor(Color.BLACK); 
     webView.loadUrl("file:///android_asset/Help.html"); 

     webView.setWebViewClient(new GameClient()); 

     customDialog_Update = (Button) dialog.findViewById(R.id.submit); 
     customDialog_Dismiss = (Button) dialog.findViewById(R.id.buttonone); 

     customDialog_Update.setOnClickListener(customDialog_UpdateOnClickListener);  customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener); 
     break; 
    } 
    return dialog; 
} 

public class GameClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 
} 
} 

とxml

<?xml version="1.0" encoding="utf-8"?> 
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="290dp" android:background="#ffffff" 
android:layout_height="350dp" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_margin="15dp" 
    android:orientation="vertical" 
    android:padding="2dp" > 

    <ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbarSize="12dip" 
    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb" 
    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <WebView android:id="@+id/webview" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

     </LinearLayout>   

      <Button 
       android:id="@+id/backbtn" android:layout_marginTop="20dp" 
       android:layout_width="165dp" 
       android:layout_height="57dp" 
       android:layout_gravity="center_horizontal"   
       android:text="Call and Talk with a buyer now" 
       android:textColor="#000000" 
       android:textSize="13sp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/submit" android:layout_marginTop="20dp" 
       android:layout_width="165dp" 
       android:layout_height="57dp" 
       android:layout_gravity="center_horizontal"     
       android:text="Email Buyer now to confirm selling details" 
       android:textColor="#000000" 
       android:textSize="13sp" 
       android:textStyle="bold" />  
    </LinearLayout> 
</ScrollView>  
</RelativeLayout> 
<RelativeLayout 
android:layout_width="290dp" android:background="@drawable/abg" 
android:layout_height="350dp" android:layout_margin="1dp"> 
<Button 
    android:id="@+id/buttonone" 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:layout_alignParentRight="true" 
    android:layout_margin="5dp" 
    android:background="@drawable/round_button_background" 
    android:gravity="center_vertical|center_horizontal" 
    android:onClick="cancelActivity" 
    android:text="X" 
    android:textColor="#ffffff" 
    android:textSize="13sp" 
    android:textStyle="bold" /> 

</RelativeLayout> 
</RelativeLayout> 

エラーログは

05-02 10:00:04.922: E/AndroidRuntime(488): FATAL EXCEPTION: main 
05-02 10:00:04.922: E/AndroidRuntime(488): java.lang.NullPointerException 
05-02 10:00:04.922: E/AndroidRuntime(488): at com.kk.TellaFriend.TellaFriend.onCreateDialog(TellaFriend.java:91) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.app.Activity.onCreateDialog(Activity.java:2472) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.app.Activity.createDialog(Activity.java:881) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.app.Activity.showDialog(Activity.java:2547) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.app.Activity.showDialog(Activity.java:2514) 
05-02 10:00:04.922: E/AndroidRuntime(488): at com.kk.TellaFriend.TellaFriend$3.onClick(TellaFriend.java:45) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.view.View.performClick(View.java:2408) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.view.View$PerformClick.run(View.java:8816) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.os.Handler.handleCallback(Handler.java:587) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.os.Looper.loop(Looper.java:123) 
05-02 10:00:04.922: E/AndroidRuntime(488): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-02 10:00:04.922: E/AndroidRuntime(488): at java.lang.reflect.Method.invokeNative(Native Method) 
05-02 10:00:04.922: E/AndroidRuntime(488): at java.lang.reflect.Method.invoke(Method.java:521) 
05-02 10:00:04.922: E/AndroidRuntime(488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-02 10:00:04.922: E/AndroidRuntime(488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-02 10:00:04.922: E/AndroidRuntime(488): at dalvik.system.NativeStart.main(Native Method) 

答えて

3

ダイアログのためにfindViewByIdを呼び出す必要があります。

WebView webView = (WebView) dialog.findViewById(R.id.webview); 
1

buttononeのIDを使用してXMLにはボタンはありませんです。それはヌルポインタがどこから来ているかです。

EDIT:onCreateDialog

customDialog_Dismiss = (Button) dialog.findViewById(R.id.backbtn);

customDialog_Dismiss = (Button) dialog.findViewById(R.id.buttonone);

を変更し、それはそれを修正する必要があります。

EDIT:ああ、複数のエラー。 @veerはこう言っています。ダイアログのfindViewByIdは、使用する正しい呼び出しであり、囲むActivityの呼び出しではありません。

+0

これは単なるダミーボタンですが、私はそれを整理しますが、それでも問題は解決しません – Karthik

関連する問題