Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将Json插入Sqlite,然后将Sqlite插入listview_Android_Json_Sqlite_Listview - Fatal编程技术网

Android 将Json插入Sqlite,然后将Sqlite插入listview

Android 将Json插入Sqlite,然后将Sqlite插入listview,android,json,sqlite,listview,Android,Json,Sqlite,Listview,我试图实现一个代码,该代码从服务器(JSON)格式获取数据,然后将其发送到Sqlite数据库,并从Sqlite发送到列表视图。然而,我得到了一个空指针验证。感谢您的帮助:) 该活动是从GCMBroadcastReceiver调用的。这可能是个问题,因为代码是自己工作的,但从广播接收器调用时就不行了 接受活动 package com.techlovejump.gcm; import java.util.ArrayList; import java.util.List; import org.a

我试图实现一个代码,该代码从服务器(JSON)格式获取数据,然后将其发送到Sqlite数据库,并从Sqlite发送到列表视图。然而,我得到了一个空指针验证。感谢您的帮助:)

该活动是从GCMBroadcastReceiver调用的。这可能是个问题,因为代码是自己工作的,但从广播接收器调用时就不行了

接受活动

package com.techlovejump.gcm;

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

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

import com.techlovejump.user.GCM.ContactListAdapter;
import com.techlovejump.user.GCM.ContactListItems;
import com.techlovejump.user.GCM.SqlDbHelper;
import com.techlovejump.user.GCM.SqlHandler;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ReceiveActivity extends Activity {

    SqlHandler sqlHandler;

    ListView lvCustomList;
    EditText IconBearing, BusName, DistanceInfo;
    Button btnsubmit;
    JSONParser jsonParser = new JSONParser();
    JSONArray products = null;
    private static String URL_Distance = "http://10.0.2.2/GET_EVERYTHING_TOGTHER/Get_Distance_From_Database.php";

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

        sqlHandler = new SqlHandler(this);
        new Get_Distance_Data().execute();
        showList();

    }

    private void showList() {

        ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
        contactList.clear();
        String query = "SELECT * FROM GPS_INFORMATION ";
        Cursor c1 = sqlHandler.selectQuery(query);
        if (c1 != null && c1.getCount() != 0) {
            if (c1.moveToFirst()) {
                do {
                    ContactListItems contactListItems = new ContactListItems();


                    contactListItems.setIcon_Bearing(c1.getString(c1
                            .getColumnIndex("Bus_Name")));
                    contactListItems.setBus_Name(c1.getString(c1
                            .getColumnIndex("Distance_Info")));
                    contactListItems.setDistance_Info(c1.getString(c1
                            .getColumnIndex("Icon_Bearing")));

                    contactList.add(contactListItems);

                } while (c1.moveToNext());
            }
        }
        c1.close();

        ContactListAdapter contactListAdapter = new ContactListAdapter(
                ReceiveActivity.this, contactList);
        lvCustomList.setAdapter(contactListAdapter);

    }

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


    class Get_Distance_Data extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(URL_Distance, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt("success");

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray("products");

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String Distance = c.getString("Distance");
                        String Image = "none";
                        String BusName = "Bus1";
                        Log.i("Distance", Distance);
                        try {
                            String query = "INSERT INTO GPS_INFORMATION(Bus_Name,Distance_Info,Icon_Bearing) values ('"
                                      + Image + "','" + BusName +"','" + Distance +"')";
                                     sqlHandler.executeQuery(query);
                        } catch (Exception e) {
                            Log.i("error","this is where the error happens");
                        }



                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

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

        }

    }

}
这是logCat

12-30 09:26:03.369: I/GCM Demo(2111): Working... 1/5 @ 25458954
12-30 09:26:03.898: I/GCM Demo(2111): Working... 2/5 @ 25459479
12-30 09:26:04.465: I/GCM Demo(2111): Working... 3/5 @ 25460046
12-30 09:26:05.036: I/GCM Demo(2111): Working... 4/5 @ 25460618
12-30 09:26:05.549: I/GCM Demo(2111): Working... 5/5 @ 25461132
12-30 09:26:06.059: I/GCM Demo(2111): Completed work @ 25461640
12-30 09:26:06.059: I/MSG(2111): New Distance data
12-30 09:26:06.059: E/msg(2111): New Distance data
12-30 09:26:06.059: I/GCM Demo(2111): Received: Bundle[{message=New Distance data, android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=616159599278}]
12-30 09:26:06.149: I/this(2111): it is working 
12-30 09:26:06.999: D/AndroidRuntime(2111): Shutting down VM
12-30 09:26:06.999: W/dalvikvm(2111): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-30 09:26:07.119: E/AndroidRuntime(2111): FATAL EXCEPTION: main
12-30 09:26:07.119: E/AndroidRuntime(2111): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techlovejump.gcm/com.techlovejump.gcm.ReceiveActivity}: java.lang.NullPointerException
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.os.Looper.loop(Looper.java:137)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at java.lang.reflect.Method.invoke(Method.java:525)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at dalvik.system.NativeStart.main(Native Method)
12-30 09:26:07.119: E/AndroidRuntime(2111): Caused by: java.lang.NullPointerException
12-30 09:26:07.119: E/AndroidRuntime(2111):     at com.techlovejump.gcm.ReceiveActivity.showList(ReceiveActivity.java:81)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at com.techlovejump.gcm.ReceiveActivity.onCreate(ReceiveActivity.java:49)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.Activity.performCreate(Activity.java:5133)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-30 09:26:07.119: E/AndroidRuntime(2111):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-30 09:26:07.119: E/AndroidRuntime(2111):     ... 11 more
12-30 09:26:09.999: D/dalvikvm(2111): GC_FOR_ALLOC freed 129K, 5% free 4255K/4464K, paused 129ms, total 129ms

lvCustomList
为空。你从未初始化过它

这时您会得到错误:

lvCustomList.setAdapter(contactListAdapter);

在onCreate方法上调用setContentView后,必须初始化它。

原因:java.lang.NullPointerException 12-30 09:26:07.119:E/AndroidRuntime(2111):在com.techlovejump.gcm.ReceiveActivity.showList(ReceiveActivity.java:81)
-第81行?我知道错误在哪里,但我不知道如何修复它:/
lvCustomList.setAdapter(contactListAdapter);