馬上加入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本身問題),準確而言,出現以下錯誤:- 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
- 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)
- 2024-03-01 21:37:28.898 7531-7665 System.err com.example.sahelper W at android.os.MessageQueue.nativePollOnce(Native Method)
- 2024-03-01 21:37:28.899 7531-7665 System.err com.example.sahelper W at android.os.MessageQueue.next(MessageQueue.java:335)
- 2024-03-01 21:37:28.899 7531-7665 System.err com.example.sahelper W at android.os.Looper.loopOnce(Looper.java:161)
- 2024-03-01 21:37:28.899 7531-7665 System.err com.example.sahelper W at android.os.Looper.loop(Looper.java:288)
- 2024-03-01 21:37:28.899 7531-7665 System.err com.example.sahelper W at android.os.HandlerThread.run(HandlerThread.java:67)
- 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:
- public class MainActivity extends AppCompatActivity
- {
- HTMLDataBase myDB1;
- ContentTableDataBase myDB2;
- CloudTableDataBase myDB3;
- InterviewDataBase myDB4;
- Context context;
- boolean check;
- private WebView view;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myDB1 = new HTMLDataBase(this); // This is the HTML DataBase
- myDB2 = new ContentTableDataBase(this); // This is the Content Table DataBase
- myDB3 = new CloudTableDataBase(this); // This is the Cloud DataBase
- myDB4 = new InterviewDataBase(this); // This is the Interview DataBase
- Cursor res = myDB1.getAllData();
- if (res.getCount() == 0)
- {
- default_data();
- }
- display_html();
- Javascript_Android_Communication();
- }
- public void display_html()
- {
- WebView view = (WebView) findViewById(R.id.WebView);
- view.getSettings().setJavaScriptEnabled(true);
- view.getSettings().setDomStorageEnabled(true);
- view.loadUrl("file:///android_asset/index.html");
- }
- public void Javascript_Android_Communication()
- {
- WebView view = (WebView) findViewById(R.id.WebView);
- view.setWebChromeClient(new WebChromeClient() {});
- WebSettings settings = view.getSettings();
- settings.setJavaScriptEnabled(true);
- settings.setDomStorageEnabled(true);
- view.addJavascriptInterface(new JavaScriptInterface(this), "Android");
- view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
- }
- public void default_data()
- {
- boolean isInserted =
- // html_element && state data
- // 1st data to 5th data
- myDB1.insertData("1", "duty_selection_area", "block") &&
- myDB1.insertData("2", "observer_shift_area", "none")&&
- myDB1.insertData("3", "telecom_shift_area", "none")&&
- myDB1.insertData("4", "duty_variable", "undefined")&&
- myDB1.insertData("5", "shift_variable", "undefined")&&
- // 6th data to 10th data
- myDB1.insertData("6", "toolbar_area", "none")&&
- myDB1.insertData("7", "clock_area", "none")&&
- myDB1.insertData("8", "duty_content_area", "none")&&
- myDB1.insertData("9", "weather_draft_background_area", "none")&&
- myDB1.insertData("10", "weather_draft_area", "none")&&
- // 11th data to 16th data
- myDB1.insertData("11", "weather_draft_table", "none")&&
- myDB1.insertData("12", "weather_table", "none")&&
- myDB1.insertData("13", "manual_observation_draft_table", "none")&&
- myDB1.insertData("14", "manual_observation_table", "none")&&
- myDB1.insertData("15", "non_mercury_ordinary_thermometers_draft_table", "none")&&
- myDB1.insertData("16", "non_mercury_ordinary_thermometers_table", "none")&&
- // 17th data to 24th data
- myDB1.insertData("17", "copy_button", "none")&&
- myDB1.insertData("18", "empty_button", "none")&&
- myDB1.insertData("19", "add_layer_button", "none")&&
- myDB1.insertData("20", "submit_button", "none")&&
- myDB1.insertData("21", "past_record_button", "none")&&
- myDB1.insertData("22", "manual_observation_submit_button", "none")&&
- myDB1.insertData("23", "disposable_submit_button", "none")&&
- myDB1.insertData("24", "partial_disposable_submit_button", "none")&&
- // 24th data to 27th data
- myDB1.insertData("25", "electronic_media_interview_area", "none")&&
- myDB1.insertData("26", "telephone_enquiry_area", "none")&&
- myDB1.insertData("27", "Tin", "none");
- }
- public void default_view()
- {
- Cursor res = myDB1.getAllData();
- if (res.getCount() > 0) {
- StringBuffer buffer = new StringBuffer();
- while (res.moveToNext())
- {
- buffer.append(res.getString(0) + "," + " ");
- buffer.append(res.getString(1) + "," + " ");
- buffer.append(res.getString(2) + "," + "
- ");
- }
- showMessage("Data", buffer.toString().trim().substring(0, buffer.toString().trim().length() - 1));
- }
- }
- public void showMessage(String title, String Message)
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setCancelable(true);
- builder.setTitle(title);
- builder.setMessage(Message);
- builder.show();
- }
- public void html_state_reader()
- {
- Cursor res = myDB1.getAllData();
- if (res.getCount() > 0) {
- StringBuffer buffer = new StringBuffer();
- while (res.moveToNext())
- {
- buffer.append(res.getString(0) + ",");
- buffer.append(res.getString(1) + ",");
- buffer.append(res.getString(2) + ",");
- }
- String[] result = buffer.toString().substring(0, buffer.toString().trim().length() - 1).split(",", -2);
- if (result[2] != "block")
- {
- view.loadUrl("javascript:resume_state()");
- }
- //showMessage("Test", result[2]);
- }
- }
- }
複製代碼
以下乃 js發生錯誤的地方
- function delete_duty(delete_parameter, coefficient)
- {
- if ( (delete_parameter == "home") && (coefficient == "all") )
- {
- myVar = Android.show_pureContentTableDataBase().split(",");
-
- for(a = myVar.length / 4 - 1; a >= 0 ; a--)
- {
- app.delete_duty(String(myVar[4 * a])); //此乃發生錯誤的地方,database 內資料無法被更新
- }
- myVar = null;
- }
- if (delete_parameter == "delete_row")
- {
- app.delete_duty(String(coefficient));
- }
- }
複製代碼
小弟想知道到底錯誤發生在甚麼地方以及如何改正,望各位大神不吝賜教。
|

|