綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] Android Js 溝通問題

[複製連結] 查看: 3598|回覆: 0|好評: 0
跳轉到指定樓層
樓主
單車浪子 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2024-3-1 21:47

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
各位大神好,小弟剛剛學習android,想寫一個app自用,但遇到問題,希望各位大神賜教。

小弟寫了一個html, css 和 js檔,然後使用android studio 放在webview運行,讓用家在使用它時能不斷更新背後的database,一切都運作暢順,但出現了一個問題:

由於app在背景閑置一段時間後會被後台關閉,所以小弟寫了幾個database 儲存信息,當用家使用html 時,js 會根據輸入更新database 的資料,然後若用家離開後,即使app 被後台關閉,亦可以在用家再開啟app 時使用database 的資料重新構建整個html,問題出現在當後台關閉app 之後,雖然可以調用database 的資料重建html,但無法再透過js 更新database (小弟相當確定在apk 被後台關閉之前 js能正確執行,所以應該不是 js本身問題),準確而言,出現以下錯誤:
  1. 2024-03-01 21:37:28.897  7531-7665  System.err              com.example.sahelper                 W  java.lang.NullPointerException: Attempt to invoke virtual method "void com.example.sahelper.ContentTableDataBase.deleteData(java.lang.String)" on a null object reference
  2. 2024-03-01 21:37:28.898  7531-7665  System.err              com.example.sahelper                 W          at com.example.sahelper.WebViewJavaScriptInterface.delete_duty(WebViewJavaScriptInterface.java:81)
  3. 2024-03-01 21:37:28.898  7531-7665  System.err              com.example.sahelper                 W          at android.os.MessageQueue.nativePollOnce(Native Method)
  4. 2024-03-01 21:37:28.899  7531-7665  System.err              com.example.sahelper                 W          at android.os.MessageQueue.next(MessageQueue.java:335)
  5. 2024-03-01 21:37:28.899  7531-7665  System.err              com.example.sahelper                 W          at android.os.Looper.loopOnce(Looper.java:161)
  6. 2024-03-01 21:37:28.899  7531-7665  System.err              com.example.sahelper                 W          at android.os.Looper.loop(Looper.java:288)
  7. 2024-03-01 21:37:28.899  7531-7665  System.err              com.example.sahelper                 W          at android.os.HandlerThread.run(HandlerThread.java:67)
  8. 2024-03-01 21:37:28.901  7531-7531  chromium                com.example.sahelper                 I  [INFO:CONSOLE(5886)] "Uncaught Error: Java exception was raised during method invocation", source: file:///android_asset/index.html (5886)
複製代碼


以下乃小弟的MainActivity:
  1. public class MainActivity extends AppCompatActivity
  2. {
  3.     HTMLDataBase myDB1;
  4.     ContentTableDataBase myDB2;
  5.     CloudTableDataBase myDB3;
  6.     InterviewDataBase myDB4;
  7.     Context context;
  8.     boolean check;

  9.     private WebView view;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState)
  12.     {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);

  15.         myDB1 = new HTMLDataBase(this); // This is the HTML DataBase
  16.         myDB2 = new ContentTableDataBase(this); // This is the Content Table DataBase
  17.         myDB3 = new CloudTableDataBase(this); // This is the Cloud DataBase
  18.         myDB4 = new InterviewDataBase(this); // This is the Interview DataBase

  19.         Cursor res = myDB1.getAllData();
  20.         if (res.getCount() == 0)
  21.         {
  22.             default_data();
  23.         }

  24.         display_html();
  25.         Javascript_Android_Communication();

  26.     }

  27.     public void display_html()
  28.     {
  29.         WebView view = (WebView) findViewById(R.id.WebView);
  30.         view.getSettings().setJavaScriptEnabled(true);
  31.         view.getSettings().setDomStorageEnabled(true);
  32.         view.loadUrl("file:///android_asset/index.html");
  33.     }

  34.     public void Javascript_Android_Communication()
  35.     {
  36.         WebView view = (WebView) findViewById(R.id.WebView);
  37.         view.setWebChromeClient(new WebChromeClient() {});
  38.         WebSettings settings = view.getSettings();
  39.         settings.setJavaScriptEnabled(true);
  40.         settings.setDomStorageEnabled(true);

  41.         view.addJavascriptInterface(new JavaScriptInterface(this), "Android");
  42.         view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
  43.     }


  44.     public void default_data()
  45.     {
  46.         boolean isInserted =
  47.                 // html_element && state data
  48.                 // 1st data to 5th data
  49.                 myDB1.insertData("1", "duty_selection_area", "block") &&
  50.                         myDB1.insertData("2", "observer_shift_area", "none")&&
  51.                         myDB1.insertData("3", "telecom_shift_area", "none")&&
  52.                         myDB1.insertData("4", "duty_variable", "undefined")&&
  53.                         myDB1.insertData("5", "shift_variable", "undefined")&&

  54.                         // 6th data to 10th data
  55.                         myDB1.insertData("6", "toolbar_area", "none")&&
  56.                         myDB1.insertData("7", "clock_area", "none")&&
  57.                         myDB1.insertData("8", "duty_content_area", "none")&&
  58.                         myDB1.insertData("9", "weather_draft_background_area", "none")&&
  59.                         myDB1.insertData("10", "weather_draft_area", "none")&&

  60.                         // 11th data to 16th data
  61.                         myDB1.insertData("11", "weather_draft_table", "none")&&
  62.                         myDB1.insertData("12", "weather_table", "none")&&
  63.                         myDB1.insertData("13", "manual_observation_draft_table", "none")&&
  64.                         myDB1.insertData("14", "manual_observation_table", "none")&&
  65.                         myDB1.insertData("15", "non_mercury_ordinary_thermometers_draft_table", "none")&&
  66.                         myDB1.insertData("16", "non_mercury_ordinary_thermometers_table", "none")&&

  67.                         // 17th data to 24th data
  68.                         myDB1.insertData("17", "copy_button", "none")&&
  69.                         myDB1.insertData("18", "empty_button", "none")&&
  70.                         myDB1.insertData("19", "add_layer_button", "none")&&
  71.                         myDB1.insertData("20", "submit_button", "none")&&
  72.                         myDB1.insertData("21", "past_record_button", "none")&&
  73.                         myDB1.insertData("22", "manual_observation_submit_button", "none")&&
  74.                         myDB1.insertData("23", "disposable_submit_button", "none")&&
  75.                         myDB1.insertData("24", "partial_disposable_submit_button", "none")&&


  76.                         // 24th data to 27th data
  77.                         myDB1.insertData("25", "electronic_media_interview_area", "none")&&
  78.                         myDB1.insertData("26", "telephone_enquiry_area", "none")&&
  79.                         myDB1.insertData("27", "Tin", "none");
  80.     }


  81.     public void default_view()
  82.     {
  83.         Cursor res = myDB1.getAllData();

  84.         if (res.getCount() > 0) {
  85.             StringBuffer buffer = new StringBuffer();
  86.             while (res.moveToNext())
  87.             {
  88.                 buffer.append(res.getString(0) + "," + "        ");
  89.                 buffer.append(res.getString(1) + "," + "        ");
  90.                 buffer.append(res.getString(2) + "," + "
  91. ");
  92.             }
  93.             showMessage("Data", buffer.toString().trim().substring(0, buffer.toString().trim().length() - 1));
  94.         }
  95.     }


  96.     public void showMessage(String title, String Message)
  97.     {
  98.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  99.         builder.setCancelable(true);
  100.         builder.setTitle(title);
  101.         builder.setMessage(Message);
  102.         builder.show();
  103.     }

  104.     public void html_state_reader()
  105.     {
  106.         Cursor res = myDB1.getAllData();

  107.         if (res.getCount() > 0) {
  108.             StringBuffer buffer = new StringBuffer();
  109.             while (res.moveToNext())
  110.             {
  111.                 buffer.append(res.getString(0) + ",");
  112.                 buffer.append(res.getString(1) + ",");
  113.                 buffer.append(res.getString(2) + ",");
  114.             }
  115.             String[] result = buffer.toString().substring(0, buffer.toString().trim().length() - 1).split(",", -2);

  116.             if (result[2] != "block")
  117.             {
  118.                 view.loadUrl("javascript:resume_state()");
  119.             }
  120.             //showMessage("Test", result[2]);
  121.         }
  122.     }
  123. }
複製代碼

以下乃 js發生錯誤的地方
  1. function delete_duty(delete_parameter, coefficient)
  2.         {
  3.                 if ( (delete_parameter == "home") && (coefficient == "all") )
  4.                 {
  5.                         myVar = Android.show_pureContentTableDataBase().split(",");
  6.                        
  7.                         for(a = myVar.length / 4 - 1; a >= 0 ; a--)
  8.                         {
  9.                                 app.delete_duty(String(myVar[4 * a])); //此乃發生錯誤的地方,database 內資料無法被更新
  10.                         }
  11.                         myVar = null;
  12.                 }

  13.                 if (delete_parameter == "delete_row")
  14.                 {
  15.                         app.delete_duty(String(coefficient));
  16.                 }
  17.         }
複製代碼

小弟想知道到底錯誤發生在甚麼地方以及如何改正,望各位大神不吝賜教。
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏1 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則