1

我正在尝试在自定义对话框中显示一个 webview,它在对话框之外工作。下面是我的代码以及自定义对话框 xml 的 xml。请给我你的专业知识,谢谢。

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)
4

2 回答 2

3

您需要调用 findViewById 进行对话。

WebView webView = (WebView) dialog.findViewById(R.id.webview);
于 2012-05-02T05:40:30.343 回答
1

您的 XML 中没有 ID 为 的 Button buttonone。这就是空指针的来源。

编辑:改变

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

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

onCreateDialog这应该解决它。

编辑:啊,多个错误。确实正如@veer 所说:对话框findViewById是正确的使用调用,而不是封闭Activity的。

于 2012-05-02T04:58:45.603 回答