2012-03-14 62 views
0

我保持我的警報和對話框在一個單獨的類,以防止混亂。在我的活動中,我有一個webView,並且HTML文檔有一個按鈕,可以在Android中執行一個自定義對話框。我正在使用JavaScriptInterface在HTML文檔和Android Java方法中的JavaScript函數之間進行通信。除了這個自定義對話框之外,這一切都可以正常使用我的Toast消息和其他功能。自定義對話框執行NPE

UPDATE:

我做了一些變化,現在至少我得到我不能遵循NPE。我將Dialog方法移到了JavaInterface中,以方便調試和其他幾個chage;請參閱代碼中的註釋。

我不知道我有什麼問題。 LogCat中沒有這個錯誤信息;只是強制關閉應用程序...... ?? Plz看看下面的代碼。

Thnx爲您提供幫助!

的logcat:

小姐,我們正在等待的WebCore的觸摸響應向下的拖累。 主題ID = 9:螺紋與未捕獲的異常退出(組= 0x4024ee20) 致命異常:WebViewCoreThread顯示java.lang.NullPointerException
在android.app.Activity.findViewById(Activity.java:1745)在 com.andaero.JavaScriptInterface。 onCreateDialog(JavaScriptInterface.java:45) at android.app.Activity.onCreateDialog(Activity.java:2758)at android.app.Activity.createDialog(Activity.java:936)at android.app.Activity.showDialog( Activity.java:2851)at android.app.Activity.showDialog(Activity.java:2810)at com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface.java:32) at android.webkit.WebViewCore.nativeTouchUp(Native Method )在 android.webkit.WebViewCore.nativeTouchUp(本機方法)在 android.webkit.WebViewCore.access $ 3600(WebViewCore.java:52)在 android.webkit.WebViewCore $ EventHub $ 1.handleMessage(WebViewCore.java:1340) 在android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:132)at android.webkit.WebViewCore $ WebCoreThread.run(WebViewCore.java:723)
在java.lang.Thread.run(Thread。的java:1020)

警報和對話框類別:

public class AlertsAndDialogs extends Activity 
{ 

    public final int CATEGORY_ID = 0; 

    . . . 
    //Moved to the JavaScriptInterface Class -----> 
    /* protected Dialog onCreateDialog(int id) 
     { 
     Dialog dialog; 
     switch (id) 
     { 
     case CATEGORY_ID: 

     AlertDialog.Builder builder; 
     Context mContext = this; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.webView)); 

     builder = new AlertDialog.Builder(mContext); 
     builder.setView(layout); 
     dialog = builder.create(); 
     break; 

     default: 
     dialog = null; 
     } 
     return dialog; 
     }*/ 

} 

的JavaScriptInterface類別:

public class JavaScriptInterface extends AlertsAndDialogs 
{ 
    public final int CATEGORY_ID = 0; 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    JavaScriptInterface(Context c) 
    { 
    mContext = c; 
    } 

    . . . 

    /** Show the DashBoard from the web page */ 
    public void showDashBoard() 
    { 
    showDialog(CATEGORY_ID); 
    } 

    protected Dialog onCreateDialog(int id) 
    { 
    Dialog dialog; 
    switch (id) 
    { 
     case CATEGORY_ID: 

     AlertDialog.Builder builder; 
     //Context mContext = this; 
     LayoutInflater inflater = (LayoutInflater)  mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.layout_root)); 

     builder = new AlertDialog.Builder(mContext); 
     builder.setView(layout); 
     dialog = builder.create(); 
     break; 

     default: 
     dialog = null; 
    } 
    return dialog; 
    } 
    . . . 
} 

在包含web視圖的主要活動:

. . . 
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
. . . 

的HTML功能:

<script type="text/javascript"> 
    function showDashBoardFunction() { 
     Android.showDashBoard(); 
    } 
</script> 

回答

1

嘗試設置

View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.layout_root)); 

View layout = inflater.inflate(R.layout.dashboard_dialog, null); 

我發現,連接視圖容器很少(如果有的話)的作品,並假設「layout_root」的東西跟你的活動,對話框無論如何都無法找到這個視圖。從AlertsAndDialogs類

+0

日Thnx。我給了一個嘗試,但NPE改爲:'at android.app.Activity.getVolumeControlStream(Activity.java:4089) \t at android.app.Dialog.setOwnerActivity(Dialog.java:206) \t at android.app .Activity.onPrepareDialog(Activity.java:2767) \t在android.app.Activity.onPrepareDialog(Activity.java:2796) \t在android.app.Activity.showDialog(Activity.java:2859) \t在機器人。 app.Activity.showDialog(Activity.java:2810) \t at com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface.java:31)' – CelticParser 2012-03-15 16:38:43

+0

Thnx再次尋求幫助,我最終將對話框重寫爲更簡單的格式。請參閱下面的答案 - 並且需要null才能按照您的建議工作。 – CelticParser 2012-03-15 21:01:51

0

刪除:

protected Dialog onCreateDialog(int id) 
     { 
     Dialog dialog; 
     switch (id) 
     { 
     case CATEGORY_ID: 

     AlertDialog.Builder builder; 
     Context mContext = this; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.webView)); 

     builder = new AlertDialog.Builder(mContext); 
     builder.setView(layout); 
     dialog = builder.create(); 
     break; 

     default: 
     dialog = null; 
     } 
     return dialog; 
     } 

,並補充一點:

public static void dashBoard(Context mContext) 
    { 
    AlertDialog.Builder builder; 
    Dialog dbDialog; 

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.dashboard_dialog, null); 

    builder = new AlertDialog.Builder(mContext); 
    builder.setView(layout); 
    dbDialog = builder.create(); 
    dbDialog.show(); 
    } 

裏面的JavaScriptInterface類,與更換電話:

public void showDashBoard() 
    { 
    AlertsAndDialogs.dashBoard(mContext); 
    }