检查我是否先连接到互联网,但我的应用程序给了我一个错误消息

时间:2014-08-04 15:12:46

标签: java android database

我正在检查我的应用是否首先有互联网连接。如果是这样,我想创建一个toast消息,并说“已连接”并执行我的任务。如果它没有连接,我希望toast消息说“Not Connected”。

这是我的问题,当我连接到互联网时,我的应用程序将声明“已连接”但应用程序很快崩溃。当我的应用程序未连接到互联网时,它将显示“未连接”(这很好,因为如果工作正常,至少该部分)。我的问题是,如何让我的应用程序不崩溃?下面是我的语法和logcat错误信息。你们有什么感想?

package com.example.connectexternaldatabase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private ListView getAllCustomerListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getAllCustomerListView = (ListView) findViewById(R.id.getAllCustomerListView);


        isThisDeviceConnectedToTheInternet();
    }

    public void isThisDeviceConnectedToTheInternet(){

        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if(networkInfo !=null && networkInfo.isConnected()){
            Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_SHORT).show();
            new GetAllCustomerTask().execute(new APIConnector());
        } else {
            Toast.makeText(getBaseContext(), "Not Connected", Toast.LENGTH_SHORT).show();
        }

    }


    public void setListAdapter(JSONArray jsonArray){
        getAllCustomerListView.setAdapter(new GetAllCustomerListViewAdapter(jsonArray, MainActivity.this));
    }

    private class GetAllCustomerTask extends AsyncTask<APIConnector, Long, JSONArray>{

        @Override
        protected JSONArray doInBackground(APIConnector... params) {
            // executed on the background thread
            return params[0].GetAllCustomers();
        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {
            setListAdapter(jsonArray);
        }



    }

}

Logcat信息

01-01 16:51:47.199: E/AndroidRuntime(4633): FATAL EXCEPTION: main
01-01 16:51:47.199: E/AndroidRuntime(4633): java.lang.NullPointerException
01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.example.connectexternaldatabase.GetAllCustomerListViewAdapter.getView(GetAllCustomerListViewAdapter.java:75)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.ListView.onMeasure(ListView.java:1127)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.View.measure(View.java:8314)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.os.Looper.loop(Looper.java:130)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at java.lang.reflect.Method.invoke(Method.java:507)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
01-01 16:51:47.199: E/AndroidRuntime(4633):     at dalvik.system.NativeStart.main(Native Method)

GetAllCustomerListViewAdapter

package com.example.connectexternaldatabase;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GetAllCustomerListViewAdapter extends BaseAdapter {

    private JSONArray dataArray;
    private Activity activity;
    private static LayoutInflater inflater = null;

    public GetAllCustomerListViewAdapter(JSONArray jsonArray, Activity a){
        this.dataArray = jsonArray;
        this.activity = a;

        inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.dataArray.length();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // set up convert View, if it's null 
        ListCell cell;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.get_all_customer_list_view_cell, null);
            cell = new ListCell();

            cell.fullName = (TextView) convertView.findViewById(R.id.customer_name);
            cell.color = (TextView) convertView.findViewById(R.id.customer_favorite_color);
            cell.facebook = (TextView) convertView.findViewById(R.id.customer_facebook);
            cell.twitter = (TextView) convertView.findViewWithTag(R.id.customer_twitter);
            cell.instagram = (TextView) convertView.findViewWithTag(R.id.customer_instagram);
            cell.mobile = (ImageView) convertView.findViewById(R.id.customer_mobile);

            convertView.setTag(cell);

        } else {
            cell = (ListCell) convertView.getTag();
        }

        try{

            JSONObject jsonObject = this.dataArray.getJSONObject(position);
            cell.fullName.setText(jsonObject.get("name")+" "+jsonObject.getString("last_name"));


            cell.color.setText("Favorite Color: "+jsonObject.getString("fav_color"));
            cell.facebook.setText("Facebook: "+ jsonObject.getString("facebook"));
            cell.twitter.setText("Twitter: "+ jsonObject.getString("twitter"));
            cell.instagram.setText("Instagram: "+ jsonObject.getString("instagram"));

            String mobile = jsonObject.getString("instagram");
            if(mobile.equals("instagram")){
                cell.mobile.setImageResource(R.drawable.ic_launcher);
            }

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


        return convertView;
    }

    private class ListCell{
        private TextView fullName;
        private TextView color;
        private TextView facebook;
        private TextView twitter;
        private TextView instagram;
        private ImageView mobile;
    }

}

0 个答案:

没有答案