为什么我的Android应用程序强制关闭,但没有导致它的错误?

问题描述:

我有一个android应用程序,连接到另一台笔记本电脑上的远程Apache服务器,它连接前几次罚款。那么现在我得到一个强制关闭窗口,但没有任何改变。为什么我的Android应用程序强制关闭,但没有导致它的错误?

这里是强制关闭

package com.thesis.menubook; 

import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import com.thesis.menubook.JSONParser; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class ChooseTable extends Activity { 

    DBConnect db = new DBConnect(this); 

    EditText tableNumber; 
    Button btnGo; 
    String table_ID; 
    String table_availability; 

// Progress Dialog 
    private ProgressDialog pDialog; 

    // JSON parser class 
    JSONParser jsonParser = new JSONParser(); 


    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_TABLE = "tabledb"; 
    private static final String TAG_TABLE_ID = "table_ID"; 
    private static final String TAG_TABLE_STATUS = "table_status"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_choose_table); 

     btnGo = (Button) findViewById(R.id.check_in_button); 
     tableNumber = (EditText) findViewById(R.id.table_number); 

     btnGo.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       table_ID = (tableNumber).getText().toString(); 
       new GetTableDetails().execute(); 
       Log.d("Table Status", table_availability); 
       if(table_availability == "AVAILABLE") 
       { 
        Toast.makeText(ChooseTable.this, table_availability, Toast.LENGTH_LONG).show(); 
        Intent i = new Intent (ChooseTable.this, ChooseOrdersMenu.class); 
        startActivity(i); 
       } 
      } 
     }); 

    } 

    /** 
    * Background Async Task to Get complete product details 
    * */ 
    class GetTableDetails extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ChooseTable.this); 
      pDialog.setMessage("Checking Table Availability. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Getting product details in background thread 
     * */ 
     protected String doInBackground(String... params) { 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        // Check for success tag 
        int success; 
        try { 
         // Building Parameter 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair(TAG_TABLE_ID, table_ID)); 
         //ipaddress of server 
         String ipaddress = ""; 
         try 
         { 
          db.open(); 
          ipaddress=db.getIP(); 
          Log.d("IP Address", ipaddress); 
         } 
         catch(Exception ex) 
         { 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          db.close(); 
         } 

         if(ipaddress != "-1") 
         { 
         // single table url 
         String url_table_details = "http://"+ipaddress+":80/MenuBook/checkTable.php"; 
         Log.d("URL", url_table_details); 
         // getting product details by making HTTP request 
         // Note that product details url will use GET request 
         JSONObject json = jsonParser.makeHttpRequest(
           url_table_details, "GET", params); 

         // check your log for json response 
         Log.d("Check Table", json.toString()); 

         // json success tag 
         success = json.getInt(TAG_SUCCESS); 
         if (success == 1) { 
          // successfully received table details 
          JSONArray tableObj = json 
            .getJSONArray(TAG_TABLE); // JSON Array 

          // get table availability from JSON Array 
          JSONObject table = tableObj.getJSONObject(0); 
          table_availability = table.getString(TAG_TABLE_STATUS); 


         }else{ 
          table_availability = "TABLE NOT FOUND"; 
         } 
         } 
         else 
         { 
          table_availability = "FAILED TO RETRIEVE SERVER IP ADDRESS"; 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 

      pDialog.dismiss(); 

     } 
    } 

} 

这里是调用之前的活动

package com.thesis.menubook; 



import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class IPAddress extends Activity { 

    DBConnect db = new DBConnect(this); 
    String ip; 
    Boolean next = false; 

    EditText ipaddress; 
    Button connect; 

    // Progress Dialog 
    private ProgressDialog pDialog; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ipaddress); 

     connect = (Button) findViewById(R.id.connectBtn); 


     connect.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
        ipaddress = (EditText) findViewById(R.id.ipAddress); 
        ip = ipaddress.getText().toString(); 
        new InsertIPAddress().execute(); 
        if(next == true) 
        { 
         Intent i = new Intent (IPAddress.this, ChooseTable.class); 
         startActivity(i); 
        } 
       } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_ipaddress, menu); 
     return true; 
    } 

    /** 
    * Background Async Task to Insert IP address 
    * */ 
    class InsertIPAddress extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(IPAddress.this); 
      pDialog.setMessage("Connecting to Server. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Inserting IPAddress in background thread 
     * */ 
     protected String doInBackground(String... params) { 

      runOnUiThread(new Runnable() { 
       public void run() { 

        pDialog.setMessage("Saving IP Address. Please wait..."); 
        long id = 0; 
        if((ip == "") || (ip.contains(" ") == true)) 
        { 
         Toast.makeText(IPAddress.this, "Please enter the server IP address and/or remove spaces.", Toast.LENGTH_LONG).show(); 
         next = false; 
        } 
        else 
        { 
         try 
         { 
          db.open(); 
          id = db.insertIPAddress(ip);  
         } 
         catch (Exception e) 
         { 
          e.printStackTrace(); 
         } 
         finally 
         { 
          db.close(); 
         } 
         Toast.makeText(IPAddress.this, "Inserted at row "+id , Toast.LENGTH_LONG).show(); 
         next = true; 
        } 
       } 
      }); 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 
     } 
    } 

} 

而这里活动的活动是我的logcat:

01-31 21:30:59.082: W/KeyCharacterMap(588): No keyboard for id 0 
01-31 21:30:59.082: W/KeyCharacterMap(588): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
01-31 21:31:20.982: D/AndroidRuntime(588): Shutting down VM 
01-31 21:31:20.982: W/dalvikvm(588): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
01-31 21:31:20.991: E/AndroidRuntime(588): FATAL EXCEPTION: main 
01-31 21:31:20.991: E/AndroidRuntime(588): java.lang.NullPointerException: println needs a message 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.util.Log.println_native(Native Method) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.util.Log.d(Log.java:122) 
01-31 21:31:20.991: E/AndroidRuntime(588): at com.thesis.menubook.ChooseTable$1.onClick(ChooseTable.java:60) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.view.View.performClick(View.java:2408) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.view.View$PerformClick.run(View.java:8816) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.os.Handler.handleCallback(Handler.java:587) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.os.Looper.loop(Looper.java:123) 
01-31 21:31:20.991: E/AndroidRuntime(588): at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-31 21:31:20.991: E/AndroidRuntime(588): at java.lang.reflect.Method.invokeNative(Native Method) 
01-31 21:31:20.991: E/AndroidRuntime(588): at java.lang.reflect.Method.invoke(Method.java:521) 
01-31 21:31:20.991: E/AndroidRuntime(588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-31 21:31:20.991: E/AndroidRuntime(588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-31 21:31:20.991: E/AndroidRuntime(588): at dalvik.system.NativeStart.main(Native Method) 
01-31 21:31:23.643: I/Process(588): Sending signal. PID: 588 SIG: 9 
+0

你有没有调试呢?在发生的地方缩小范围。 – Cornelius

01 -31 21:31:20.991:E/AndroidRuntime(588):致命例外:主01-31 21:31:20.991:E/AndroidRuntime(588):显示java.lang.NullPointerException: 的println需要一个消息

这告诉你,它是由

问题是造成在这里:

DBConnect db = new DBConnect(this); 

没有的onCreate()函数之前初始化,您可以定义数据库连接在你的类机构,但对其进行初始化上的onCreate()函数

+0

我遵循你的建议,现在它正在拖延很长时间来回应,这提示我强制关闭它 –

+0

也许有更多的错误,因为其他人在你的代码中提到你必须解决它们,但我100%确定在使用(this)之前,在Activity定义中的oncreate函数会导致NPE,发布你更新的logcat,也许我们可以帮助你更多。Goodluck –

+0

其实在它强制关闭之后,因为我提示等待活动并强制关闭自身,LogCat没有任何日志 –

这不是一个明确的错误?

它字面上说,你有一个println或日志,其中消息不知道/ null。

我猜在

Log.d("Table Status", table_availability); 

table_availability是空

你不能登录空

改变这一行

Log.d("Table Status", table_availability); 

Log.d("Table Status", table_availability+""); 

或进行if条件

if(table_availability!=null){ 
Log.d("Table Status", table_availability); 
} 
+0

我把所有我已初始化的日志上的空字符串。而且我还在异步任务上放置了'DBConnect db = new DBConnect(ActivityName.this);',现在需要很长时间才能加载并提示我强制关闭 –

+0

请随时添加一个新问题你有什么 。 – confucius