0

我正在实现一个Activity带有button“珠宝和配饰”的文本。当我传递按钮的文本并触发Intent它时,它会导致NullPointerException另一个Activity?该怎么办?任何帮助将不胜感激。提前致谢。

public class Listing extends ListActivity {
/** Button to provide advance search functionality. */

TextView Paging;
String tit[];
String price1[];
String brand[];
String value;
String pst;
TextView text;
List<ListRowData> listData;
String priceAtPosition;
String typeAtPosition;
String brandAtPosition;
ProgressDialog dialog1;
ProgressDialog dialog;
AlertDialog alert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.item_listing_page);
    Intent intent = getIntent();
    savedInstanceState = intent.getExtras();
    value = savedInstanceState.getString("value");
    priceAtPosition = savedInstanceState.getString("priceAtPosition");
    typeAtPosition = savedInstanceState.getString("typeAtPosition");
    brandAtPosition = savedInstanceState.getString("brandAtpositoion");

    BottomBarActivity logoBar = (BottomBarActivity) 
                                findViewById(R.id.bottomBarLayout);
    logoBar.setActivity(this);

    TextView txAreaName = (TextView) findViewById(R.id.areaName);
    txAreaName.setText("Shop");

    text = (TextView) findViewById(R.id.list_heading);
    text.setText(value);

    LoginScreenData loginScreenData = new LoginScreenData();

    pst = loginScreenData.getPst();
    Paging = (TextView) findViewById(R.id.textPage);

    Button refine = (Button) findViewById(R.id.bt_refine);
    refine.setOnClickListener(btClickListener);

    Button sort = (Button) findViewById(R.id.bt_sort);
    sort.setOnClickListener(btClickListener);

    Button btnSearch = (Button) findViewById(R.id.bt_search);
    btnSearch.setOnClickListener(btSearchListener);

    String Latest1 = "Latest";

    listData = new ArrayList<ListRowData>();

    methodApiCall(pst, Latest1);

// Get the intent, verify the action and get the query

    Intent in = getIntent();
    in.putExtra("value", value);
    if (Intent.ACTION_SEARCH.equals(in.getAction())) {
        String query = in.getStringExtra(SearchManager.QUERY);
        methodApiSearchCall(pst, query);
    }

}

private void methodApiSearchCall(String pst2, String query) {

    List<ListRowData> listData;
    listData = new ArrayList<ListRowData>();

    String envelope1 = "<req>" + "<term>" + query + "</term>" + "</req>";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
    request.addProperty("requestXml", envelope1);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    Object response = null;
    String searchReference[] = null;
    String eventSearchResponse = null;
    String searchImg[] = null;
    String searchTitle[] = null;

    try {
        androidHttpTransport.call(SOAP_ACTION1, envelope);
        response = envelope.getResponse();
        eventSearchResponse = response.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(
                eventSearchResponse)));

        // normalize the document
        doc.getDocumentElement().normalize();

        NodeList nodelistDet = doc.getElementsByTagName("det");

        for (int j = 0; j < nodelistDet.getLength(); j++) {

            Node node1 = nodelistDet.item(0);
            Element fstelement1 = (Element) node1;
            NodeList lnlist = fstelement1.getElementsByTagName("ln");
            searchReference = new String[lnlist.getLength()];
            searchImg = new String[lnlist.getLength()];
            searchTitle = new String[lnlist.getLength()];

            for (int i = 0; i < lnlist.getLength(); i++) {

                Element lnelement2 = (Element) lnlist.item(i);
                searchReference[i] = lnelement2.getAttribute("ref");
                searchImg[i] = lnelement2.getAttribute("img");
                searchTitle[i] = lnelement2.getAttribute("tit");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

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

        ListRowData rowData = new ListRowData();
        rowData.setSearchReference(searchReference[i]);
        rowData.setSearchImg(searchImg[i]);
        rowData.setSearchTitle(searchTitle[i]);
        listData.add(rowData);
    }
    method(listData);

}

public void methodApiCall(String pstcode, String latest) {

    List<ListRowData> listData = new ArrayList<ListRowData>();
    String envelope1 = "<req>" + "<pst>" + pstcode + "</pst>" + "<sort>"
            + latest + "</sort>" + "<dept>WOMEN</dept>" + "<cat>" + value
            + "</cat>" + "<prmax>" + priceAtPosition + "</prmax>"
            + "<brand>" + brandAtPosition + "</brand>" + "<pp>10</pp>"
            + "<inx>1</inx>" + "</req>";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("requestXml", envelope1);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    Object response = null;
    String[] description = null;
    String salePrice[] = null;
    String reference[] = null;
    String image[] = null;
    String shopSearchResponse = null;

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
        shopSearchResponse = response.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(
                shopSearchResponse)));
        // normalize the document
        doc.getDocumentElement().normalize();
        NodeList rootnode = doc.getElementsByTagName("resp");
        Node node = rootnode.item(0);
        Element fstelement = (Element) node;
        NodeList reglist = fstelement.getElementsByTagName("cnt").item(0)
                .getChildNodes();
        Node nvalue = (Node) reglist.item(0);
        NodeList reglist1 = fstelement.getElementsByTagName("pp").item(0)
                .getChildNodes();
        Node nvalue1 = (Node) reglist1.item(0);
        NodeList reglist2 = fstelement.getElementsByTagName("inx").item(0)
                .getChildNodes();
        Node nvalue2 = (Node) reglist2.item(0);
        String count = nvalue.getNodeValue();
        String index = nvalue2.getNodeValue();
        String perPage = nvalue1.getNodeValue();
        Paging.setText(index + " - " + perPage + " out of " + count);
        NodeList nodelist = doc.getElementsByTagName("det");

        for (int j = 0; j < nodelist.getLength(); j++) {

            Node node1 = nodelist.item(0);
            Element fstelement1 = (Element) node1;
            NodeList lnlist = fstelement1.getElementsByTagName("ln");
            description = new String[lnlist.getLength()];
            tit = new String[lnlist.getLength()];
            salePrice = new String[lnlist.getLength()];
            reference = new String[lnlist.getLength()];
            price1 = new String[lnlist.getLength()];
            brand = new String[lnlist.getLength()];
            image = new String[lnlist.getLength()];

            for (int i = 0; i < lnlist.getLength(); i++) {

                Element lnelement = (Element) lnlist.item(i);
                description[i] = lnelement.getAttribute("desc");
                tit[i] = lnelement.getAttribute("tit");
                salePrice[i] = lnelement.getAttribute("sale");
                reference[i] = lnelement.getAttribute("ref");
                price1[i] = lnelement.getAttribute("price");
                brand[i] = lnelement.getAttribute("brand");
                image[i] = lnelement.getAttribute("img");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

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

        ListRowData rowData = new ListRowData();
        rowData.setDescription(description[i]);
        rowData.setNew_Price(salePrice[i]);
        rowData.setOld_Price(price1[i]);
        rowData.setTitle(tit[i]);
        rowData.setImage(image[i]);
        listData.add(rowData);
    }

    TextView totalitem = (TextView) findViewById(R.id.tx_totalItem);
    totalitem.setText(Integer.toString(tit.length) + " Items");
    ListView lv = getListView(); // Getting list view to activity
    setListAdapter(new ListCustomAdapter(this,
            R.layout.shop_list_items_row, listData));

    // sets onItemClick event on list item
    lv.setOnItemClickListener(itemClickListener);

}

private OnClickListener btSearchListener = new OnClickListener() {

    public void onClick(View view) {
        Intent in = new Intent();
        in.putExtra("value", value);
        onSearchRequested();
    }
};

/** OnClickListener instance to perform onClick on buttons. */

private OnClickListener btClickListener = new OnClickListener() {

    public void onClick(View v) {
        Intent myIntent;
        if (v.getId() == R.id.bt_refine) {
            myIntent = new Intent().setClass(getApplicationContext(),
                    RefinePageActivity.class);
            myIntent.putExtra("title", tit);
            myIntent.putExtra("price", price1);
            myIntent.putExtra("brand", brand);
            myIntent.putExtra("value1", value);
            startActivity(myIntent);
        }

        else if (v.getId() == R.id.bt_sort) {

            final String items[] = { "Latest", "Title", "Price" };
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    Listing.this);
            builder.setTitle("Pick a Item for sort");
            builder.setSingleChoiceItems(items, -1,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            listData = new ArrayList<ListRowData>();
                            methodApiCall("Default", items[item]);
                            alert.dismiss();
                            dialog1 = ProgressDialog.show(Listing.this,
                                    "loading please wait",
                                    "Loading. Please wait...", true);
                            new Thread() {
                                public void run() {
                                    try {
                                        // performBackgroundProcess1();
                                        sleep(3000, 000);
                                    } catch (Exception e) {
                                        Log.e("tag", e.getMessage());
                                    }
                                    // dismiss the progress dialog
                                    dialog1.dismiss();

                                }
                            }.start();
                        }
                    });
            alert = builder.create();
            alert.show();
        }
    }
};

void method(List<ListRowData> ll) {

    setListAdapter(new ListCustomAdapterShopSearch(this,
            R.layout.events_search_list_item_row, ll));
    ListView lv1 = getListView();
    lv1.setOnItemClickListener(itemClickListener);
}

/** OnItemClickListener instance to perform itemClick event on items. */
private OnItemClickListener itemClickListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            final int position, long id) {

        dialog = ProgressDialog.show(view.getContext(),
                "loading please wait", "Loading. Please wait...", true);
        new Thread() {
            public void run() {
                try {
                    Intent inDetail = new Intent().setClass(
                            getApplicationContext(), DetailPage.class);
                    inDetail.putExtra("Position", position);
                    startActivity(inDetail);
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
                // dismiss the progress dialog
                dialog.dismiss();
            }
        }.start();
    }
};
}

日志输出:

05-24 12:02:49.232: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstrov/commerce.app.lib.shop.Listing}: java.lang.NullPointerException
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.os.Looper.loop(Looper.java:123)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-24 12:02:49.232: E/AndroidRuntime(849):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 12:02:49.232: E/AndroidRuntime(849):  at java.lang.reflect.Method.invoke(Method.java:521)
05-24 12:02:49.232: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-24 12:02:49.232: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-24 12:02:49.232: E/AndroidRuntime(849):  at dalvik.system.NativeStart.main(Native Method)
05-24 12:02:49.232: E/AndroidRuntime(849): Caused by: java.lang.NullPointerException
05-24 12:02:49.232: E/AndroidRuntime(849):  at commerce.app.lib.shop.Listing.methodApiCall(Listing.java:280)
05-24 12:02:49.232: E/AndroidRuntime(849):  at commerce.app.lib.shop.Listing.onCreate(Listing.java:110)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-24 12:02:49.232: E/AndroidRuntime(849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-24 12:02:49.232: E/AndroidRuntime(849):  ... 11 more
4

2 回答 2

0
Encode & with :    &amp;
于 2012-05-24T06:07:04.243 回答
0

Java中的所有字符串都存储在UNICODE中,因此使用char(&)不会抛出异常。发布空指针异常的堆栈跟踪,可能还有其他问题。

于 2012-05-24T06:09:21.033 回答