0

我有一个 android 应用程序,它连接到另一台笔记本电脑上的远程 Apache 服务器,并且在前几次连接得很好。然后现在我得到一个强制关闭窗口,但没有任何改变。

这是强制关闭的活动

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
4

4 回答 4

2

01-31 21:31:20.991: E/AndroidRuntime(588): 致命异常: main 01-31 21:31:20.991: E/AndroidRuntime(588): java.lang.NullPointerException: println 需要一条消息

这告诉你,它是由什么引起的

于 2013-01-31T13:41:04.577 回答
2

你不能记录一个空

更改此行

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

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

或做一个 if 条件

if( table_availability!=null){
 Log.d("Table Status", table_availability);
}
于 2013-01-31T13:41:53.757 回答
1

问题在这里:

DBConnect db = new DBConnect(this);

不会在 onCreate() 函数之前初始化,您可以在类主体中定义 DBConnect,但在 onCreate() 函数上初始化它

于 2013-01-31T13:41:23.413 回答
1

这怎么不是一个明显的错误?

从字面上看,您有一个 println 或日志,其中消息未知/为空。

我猜在

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

table_availability 为空

于 2013-01-31T13:41:24.990 回答