0

我在我的应用程序中使用列表视图,首先我从添加按钮更新列表视图,但后来我需要选择更多项目,所以我需要移动到另一个页面来选择值,所以我使用意图并将我的值带到另一个页面在数组中,然后当我尝试在另一个页面上移动以选择更多项目时,我的应用程序由于 listview 而崩溃,我尝试了没有 listview 的程序,然后它工作正常并在 logcat 中显示我的数组。

所以,这是我的代码:-

public class selectsaloon extends Activity {
public String Salon_name, Spinnervalue, Spinnervalue1, Spinnervalue2,
        Spinnervalue3, service, regular, banding, band, username, password,
        percentage, total_visits, number_of_visits, total, result1,
        btnvalue, salon, service1, price, visit;
public Spinner Spinner, Spinner1, Spinner2, Spinner3;
ArrayList<String> list, list1, list2, list3;

SimpleAdapter adp;
String str[], str1[], str2[], str3[], vik[], listv1[], listv2[], listv3[],
        listv4[];
ListView serviceview;
JSONArray jsArray;
Integer Count, cont;
JSONObject json1, json2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.selectsaloon);

    Intent in = getIntent();
    Bundle b = in.getExtras();
    cont = b.getInt("count");
    username = b.getString("username");
    password = b.getString("password");
    btnvalue = b.getString("btnvalue");
    if (cont == 0) {

        listv1 = b.getStringArray("Spinnervalue");
        listv2 = b.getStringArray("Spinnervalue1");
        listv3 = b.getStringArray("Spinnervalue2");
        listv4 = b.getStringArray("Spinnervalue3");
        list();
    }

    json();

    Spinner1 = (Spinner) findViewById(R.id.spnerservice);
    Spinner2 = (Spinner) findViewById(R.id.spnerprice);
    Spinner3 = (Spinner) findViewById(R.id.spnerdscnt);
    serviceview = (ListView) this.findViewById(R.id.serviceview);
    Button btnaddmore = (Button) findViewById(R.id.btnaddmore);
    // Spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    btnaddmore.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            list.add(Spinnervalue);

            list1.add(Spinnervalue1);

            list2.add(Spinnervalue2);

            list3.add(Spinnervalue3);
            str = list.toArray(new String[list.size()]);
            str1 = list1.toArray(new String[list1.size()]);
            str2 = list2.toArray(new String[list2.size()]);
            str3 = list3.toArray(new String[list3.size()]);

            Intent in = new Intent(getApplicationContext(),
                    selectservice1.class);
            Bundle bundle1 = new Bundle();
            bundle1.putInt("Count", 1);
            bundle1.putString("username", username);
            bundle1.putString("password", password);
            bundle1.putStringArray("Spinnervalue", str);
            bundle1.putStringArray("Spinnervalue1", str1);
            bundle1.putStringArray("Spinnervalue2", str2);
            bundle1.putStringArray("Spinnervalue3", str3);
            in.putExtras(bundle1);
            startActivity(in);
        }
    });
    Button btncontinue = (Button) findViewById(R.id.btncontinue);
    // Spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    btncontinue.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String res = null;
            try {
                JSONObject object11 = new JSONObject();
                object11.put("salon", salon);
                object11.put("service", service1);
                object11.put("price", price);
                object11.put("visit", visit);
                object11.put("username", username);

                String strng = object11.toString();
                object11 = executehttpost(strng);

            JSONArray earthquakes =   object11.getJSONArray("item");

                for (int in = 0; in < 1; in++) {

                JSONObject e = earthquakes.getJSONObject(in);
                    // String id=String.valueOf(i);
                    res = e.getString("flag");
                }

            // Toast.makeText(this, "Json Objects are : ",+ res,
                // Toast.LENGTH_LONG).show();
            if (res.contentEquals("OOPS! Record can't inserted")) {

            Intent ourintent = new Intent(getApplicationContext(),
                            MainActivity.class);

                    startActivity(ourintent);

                } else {

                Intent i1 = new Intent(getApplicationContext(),
                            selectservice.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("count", 0);
                    bundle.putString("username", username);
                    bundle.putString("password", password);
                    i1.putExtras(bundle);

                    startActivity(i1);

                }

            } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
            } catch (Exception je) {
            }

            // }
        }

        private JSONObject executehttpost(String strng) throws Exception {
            // TODO Auto-generated method stub
            InputStream inpstream = null;
            String result = "";
            JSONObject jsnarry = null;

            try {
                list.add(Spinnervalue);

                list1.add(Spinnervalue1);

                list2.add(Spinnervalue2);

                list3.add(Spinnervalue3);
                str = list.toArray(new String[list.size()]);
                str1 = list1.toArray(new String[list1.size()]);
                str2 = list2.toArray(new String[list2.size()]);
                str3 = list3.toArray(new String[list3.size()]);
                for (int i = 0; i < str.length - 1; i++) {
                    salon = str[i];
                    service1 = str1[i];
                    price = str2[i];
                    visit = str3[i];
                HttpClient client = new DefaultHttpClient();
 HttpPost post = new HttpPost(
                            "http://www.siliconsoftwares.in/griffin/jsonuserservice.php");

                    List<NameValuePair> value = new ArrayList<NameValuePair>(
                            8);

                    value.add(new BasicNameValuePair("salon", salon));
                    value.add(new BasicNameValuePair("service", service1));
                    value.add(new BasicNameValuePair("price", price));
                    value.add(new BasicNameValuePair("visit", visit));
                    value.add(new BasicNameValuePair("username", username));

                    UrlEncodedFormEntity urlentity = new UrlEncodedFormEntity(
                            value);
                    post.setEntity(urlentity);

                    HttpResponse response = client.execute(post);
                    HttpEntity resultentity = response.getEntity();

                    inpstream = resultentity.getContent();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inpstream, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                inpstream.close();
                result = sb.toString();
            } catch (Exception e) {
                // TODO: handle exception
            }
            // TODO Auto-generated method stub

            try {

                jsnarry = new JSONObject(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }

            return jsnarry;
        }

    });

    list = new ArrayList<String>();
    list1 = new ArrayList<String>();
    list2 = new ArrayList<String>();
    list3 = new ArrayList<String>();

    Button btnadd = (Button) findViewById(R.id.btnadd);
    // Spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    btnadd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            list.add(Spinnervalue);

            list1.add(Spinnervalue1);

            list2.add(Spinnervalue2);

            list3.add(Spinnervalue3);
            str = list.toArray(new String[list.size()]);
            str1 = list1.toArray(new String[list1.size()]);
            str2 = list2.toArray(new String[list2.size()]);
            str3 = list3.toArray(new String[list3.size()]);

            String[] from = { "str", "str1", "str2", "str3" };

            int[] to = { R.id.listview1, R.id.listview2, R.id.listview3,
                    R.id.listview4 };

            List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < str.length; i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("str", str[i]);
                map.put("str1", "" + str1[i]);
                map.put("str2", "" + str2[i]);
                map.put("str3", "" + str3[i]);
                fillMaps.add(map);
            }
            Log.d("bbb", "" + fillMaps);

            adp = new SimpleAdapter(getBaseContext(), fillMaps,
                    R.layout.spinnerview, from, to);
            serviceview.setAdapter(adp);
        //  Count = adp.getCount();
            //if (Count != 0) {

                //serviceview.setVisibility(View.VISIBLE);

            //}

        }
        // }
    });

}

private void list() {
    // TODO Auto-generated method stub

    String[] frm = { "str", "str1", "str2", "str3" };

    int[] tv = { R.id.listview1, R.id.listview2, R.id.listview3,
            R.id.listview4 };
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < listv1.length-1; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("str", "" + listv1[i]);
        map.put("str1", "" + listv2[i]);
        map.put("str2", "" + listv3[i]);
        map.put("str3", "" + listv4[i]);
        fillMaps.add(map);
        //Log.d("listv1", "" + listv1[i]);
        //Log.d("listv2", "" + listv2[i]);
        //Log.d("listv3", "" + listv3[i]);
        //Log.d("listv4", "" + listv4[i]);

    }
    //Log.d("aaa", "" + fillMaps);


    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), fillMaps,R.layout.spinnerview, frm, tv);
    //Count = adapter.getCount();
//  if (Count != 0) {

    //  serviceview.setVisibility(View.VISIBLE);

    //}
    serviceview.setAdapter(adapter);


}

private void json() {
    // TODO Auto-generated method stub

    JSONObject json = JSONfunctions
            .getJSONfromURL("http://siliconsoftwares.in/griffin/jsonservice.php");
    try {

        JSONArray earthquakes = json.getJSONArray("item");
        final String[] items = new String[earthquakes.length()];

        for (int i = 0; i < earthquakes.length(); i++) {

            JSONObject e = earthquakes.getJSONObject(i);
            Salon_name = e.getString("saloon_name");
            items[i] = e.getString("saloon_name");
            Spinner = (Spinner) findViewById(R.id.spnersaloon);

        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                selectsaloon.this, android.R.layout.simple_spinner_item,
                items);

        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        // R.layout.sp, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner.setAdapter(adapter);

    } catch (Exception e) {
        // TODO: handle exception
    }

    Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            Spinnervalue = parent.getItemAtPosition(pos).toString();
            // if (Spinnervalue.equals("Affinity")) {
            if (btnvalue.equals("cutting")) {
                json1 = JSONfunctions
                        .getJSONfromURL("http://siliconsoftwares.in/griffin/jsonhair.php");

            } else {

                json1 = JSONfunctions
                        .getJSONfromURL("http://siliconsoftwares.in/griffin/jsonbeauty.php");
            }
            try {

                JSONArray earthquakes1 = json1.getJSONArray("item");
                final String[] items1 = new String[earthquakes1.length()];

                for (int i = 0; i < earthquakes1.length(); i++) {

                    JSONObject e = earthquakes1.getJSONObject(i);

                    number_of_visits = e.getString("number_of_visits");
                    total_visits = e.getString("total_visits");
                    percentage = e.getString("percentage");
                    total = number_of_visits + "-" + percentage + "%";
                    items1[i] = total;
                    // Log.d("total", "" + total);

                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        selectsaloon.this, R.layout.sp, items1);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                Spinner3.setAdapter(adapter);
                spnr();

            } catch (Exception e) {
                // TODO: handle exception
            }

            if (btnvalue.equals("cutting")) {

                json2 = JSONfunctions
                        .getJSONfromURL("http://siliconsoftwares.in/griffin/jsonstylist.php?saloon_name="
                                + Spinnervalue + "");
            } else {
                json2 = JSONfunctions
                        .getJSONfromURL("http://siliconsoftwares.in/griffin/jsonstylist1.php?saloon_name="
                                + Spinnervalue + "");
            }
            try {

                JSONArray earthquakes1 = json2.getJSONArray("item");
                final String[] items1 = new String[earthquakes1.length()];

                for (int i = 0; i < earthquakes1.length(); i++) {

                    JSONObject e = earthquakes1.getJSONObject(i);

                    service = e.getString("service");
                    items1[i] = e.getString("service");

                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        selectsaloon.this, R.layout.sp, items1);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                Spinner1.setAdapter(adapter);

                spn();

            } catch (Exception e) {
                // TODO: handle exception
            }
        }

        private void spnr() {
            // TODO Auto-generated method stub
            Spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Spinnervalue3 = arg0.getItemAtPosition(arg2).toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }

            });
        }

        private void spn() {
            // TODO Auto-generated method stub
            Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent,
                        View view, int pos, long id) {
                    Spinnervalue1 = parent.getItemAtPosition(pos)
                            .toString();
                    // Log.d("Spinner1", "" + Spinnervalue1);
                    // Log.d("Spinner", "" + Spinnervalue);

                    String res = null;
                    try {
                        JSONObject object11 = new JSONObject();
                        object11.put("service", Spinnervalue1);
                        object11.put("saloon_name", Spinnervalue);

                        String strng = object11.toString();
                        object11 = executehttpost(strng);

                        JSONArray earthquakes = object11
                                .getJSONArray("item");
                        final String[] items = new String[earthquakes
                                .length()];

                        for (int i = 0; i < earthquakes.length(); i++) {

                            JSONObject e = earthquakes.getJSONObject(i);
                            regular = e.getString("regular");
                            banding = e.getString("banding");
                            band = banding + " " + "£" + regular;
                            items[i] = band;

                        }

                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                selectsaloon.this, R.layout.sp, items);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        Spinner2.setAdapter(adapter);
                        spn2();

                    } catch (JSONException e) {
                        Log.e("log_tag",
                                "Error parsing data " + e.toString());
                    } catch (Exception je) {
                    }

                }

                private void spn2() {
                    // TODO Auto-generated method stub
                    Spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0,
                                View arg1, int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            Spinnervalue2 = arg0.getItemAtPosition(arg2)
                                    .toString();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }
                    });
                }

                private JSONObject executehttpost(String strng)
                        throws Exception {
                    // TODO Auto-generated method stub

                    InputStream inpstream = null;
                    String result = "";
                    JSONObject jsnarry = null;

                    try {
                        HttpClient client = new DefaultHttpClient();
                        HttpPost post = new HttpPost(
                                "http://siliconsoftwares.in/griffin/jsonprice.php");

                        List<NameValuePair> value = new ArrayList<NameValuePair>(
                                8);
                        value.add(new BasicNameValuePair("service",
                                Spinnervalue1));
                        value.add(new BasicNameValuePair("saloon_name",
                                Spinnervalue));

                        UrlEncodedFormEntity urlentity = new UrlEncodedFormEntity(
                                value);
                        post.setEntity(urlentity);

                        HttpResponse response = client.execute(post);
                        HttpEntity resultentity = response.getEntity();

                        inpstream = resultentity.getContent();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    try {

                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(inpstream,
                                        "iso-8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        inpstream.close();
                        result = sb.toString();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    // TODO Auto-generated method stub

                    try {

                        jsnarry = new JSONObject(result);
                    } catch (JSONException e) {
                        Log.e("log_tag",
                                "Error parsing data " + e.toString());
                    }

                    return jsnarry;

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }

            });
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}

 }

`

我的错误在这里-

private void list() {
    // TODO Auto-generated method stub

    String[] frm = { "str", "str1", "str2", "str3" };

    int[] tv = { R.id.listview1, R.id.listview2, R.id.listview3,
            R.id.listview4 };
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < listv1.length-1; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("str", "" + listv1[i]);
        map.put("str1", "" + listv2[i]);
        map.put("str2", "" + listv3[i]);
        map.put("str3", "" + listv4[i]);
        fillMaps.add(map);
        //Log.d("listv1", "" + listv1[i]);
        //Log.d("listv2", "" + listv2[i]);
        //Log.d("listv3", "" + listv3[i]);
        //Log.d("listv4", "" + listv4[i]);

    }
    //Log.d("aaa", "" + fillMaps);


    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), fillMaps,R.layout.spinnerview, frm, tv);
    //Count = adapter.getCount();
//  if (Count != 0) {

    //  serviceview.setVisibility(View.VISIBLE);

    //}
    serviceview.setAdapter(adapter);


}

`

我的日志猫:-

`

04-12 10:58:20.909: I/dalvikvm(1406): threadid=3: reacting to signal 3
04-12 11:17:00.720: E/AndroidRuntime(1817): FATAL EXCEPTION: main
04-12 11:17:00.720: E/AndroidRuntime(1817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.griffinclub/com.example.griffinclub.selectsaloon}: java.lang.NullPointerException
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.os.Looper.loop(Looper.java:137)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at java.lang.reflect.Method.invoke(Method.java:511)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at dalvik.system.NativeStart.main(Native Method)
04-12 11:17:00.720: E/AndroidRuntime(1817): Caused by: java.lang.NullPointerException
04-12 11:17:00.720: E/AndroidRuntime(1817):     at com.example.griffinclub.selectsaloon.list(selectsaloon.java:341)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at com.example.griffinclub.selectsaloon.onCreate(selectsaloon.java:75)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.Activity.performCreate(Activity.java:4465)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-12 11:17:00.720: E/AndroidRuntime(1817):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-12 11:17:00.720: E/AndroidRuntime(1817):     ... 11 more
`
4

1 回答 1

2

list()您在初始化之前调用serviceView。最后一行list()尝试在它仍然null.

移动这条线:

serviceview = (ListView) this.findViewById(R.id.serviceview);

到此块之前的某个地方:

if (cont == 0) {
    listv1 = b.getStringArray("Spinnervalue");
    listv2 = b.getStringArray("Spinnervalue1");
    listv3 = b.getStringArray("Spinnervalue2");
    listv4 = b.getStringArray("Spinnervalue3");
    list();
}

可能会或可能不会给你工作代码,但它会让你超越你的当前NullPointerException(假设findViewById()能够找到View具有该 ID 的)。

于 2013-04-12T06:18:21.163 回答