2

我的代码中有一个 ListView,当我尝试单击项目时,我在 (*)< 上有一个 NullPointerException - 请参阅代码:

public class Fragment2 extends ListFragment {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// All static variables
static final String URL = "http://www.testtesttest.netsons.org/Rullino.xml";
// XML node keys
static final String KEY_ITEM = "item";
static final String KEY_ID = "ID";
static final String KEY_NOT = "not";
static final String KEY_TESTO = "testo";// parent node
ListView list;


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);




    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NOT, parser.getValue(e, KEY_NOT));
        map.put(KEY_TESTO, parser.getValue(e, KEY_TESTO));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,R.layout.list_item,new String[] { KEY_NOT, KEY_TESTO }, new int[] {R.id.not, R.id.testo });

    setListAdapter(adapter);
    View V =inflater.inflate(R.layout.ac_fragment2,null);
    list=(ListView)V.findViewById(android.R.id.list);

    // Getting adapter by passing xml data ArrayList


    // selecting single ListView item
    // listening to single listitem click
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                               int position, long id) {
            // getting values from selected ListItem
          /*  String not = ((TextView) view.findViewById(R.id.not)).getText().toString();
            String testo = ((TextView) view.findViewById(R.id.testo)).getText().toString();


            // Starting new intent
            Intent in = new Intent(getActivity().getApplicationContext(), ImageGridActivity.class);
            in.putExtra(KEY_NOT, not);
            in.putExtra(KEY_TESTO, testo);
            getActivity().startActivity(in);
*/            }

    });
    return inflater.inflate(R.layout.ac_fragment2, container, false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.i("FragmentList", "Item clicked: " + id);

    Intent in = new Intent(getActivity(), ImageGridActivity.class);
    String key = menuItems.get(position).get(KEY_NOT).toString();
    String test = menuItems.get(position).get(KEY_TESTO).toString();
    in.putExtra(KEY_NOT, key); (*)
    in.putExtra(KEY_TESTO, test); (*)
    getActivity().startActivity(in);

}

}

这是代码xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
<!-- Main ListView
     Always give id value as list(@android:id/list)
-->
<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:clickable="true"
        android:longClickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
 </LinearLayout>

list_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Name Label -->
    <TextView
        android:id="@+id/not"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#dc6800"
        android:textSize="16sp"
        android:textStyle="bold"
        android:paddingTop="6dip"
        android:paddingBottom="2dip" />
    <!-- Description label -->
    <TextView
        android:id="@+id/testo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#acacac"
        android:paddingBottom="2dip">
    </TextView>
    <!-- Linear layout for cost and price Cost: Rs.100 -->


    </LinearLayout>

我能做些什么?我没有在互联网上找到解决方案......任何人帮助:D

4

4 回答 4

0

你设置不可点击

list.setClickable(false);
    // Getting adapter by passing xml data ArrayList

而你试图点击一个项目,它不能工作。

于 2013-06-26T09:22:49.370 回答
0

你犯了两个错误:

  1. list.setClickable(false);
  2. Intent in = new Intent(getActivity(), ImageGridActivity.class);

将这两行更改如下:

list.setClickable(true); // or you can remove this line as you have set android:clickable="true" in xml layout
Intent in = new Intent(getApplicationContext(), ImageGridActivity.class);
于 2013-06-26T09:25:31.357 回答
0

尝试这个

Intent in = new Intent(getActivity().getApplicationContext(), ImageGridActivity.class);
于 2013-06-26T09:33:28.643 回答
0

你需要覆盖onListItemClick

声明menuItems为类变量。

   public class Fragment2 extends ListFragment {
   ArrayList<HashMap<String, String>> menuItems ;
   ....  
   }

然后覆盖onListItemClick

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
     Log.i("FragmentList", "Item clicked: " + id);

            Intent in = new Intent(getActivity(), ImageGridActivity.class);
            String key = menuItems.get(position).get(KEY_NOT).toString();
            String test = menuItems.get(position).get(KEY_TEST0).toString();
            in.putExtra("key1", key);
            in.putExtra("key2", test);
            getActivity().startActivity(in);

 }

注意:键必须匹配

ImageGridActivity.

    Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
        String value1 = extras.getString("key1");
        String value2 = extras.getString("key2");
    }
于 2013-06-26T10:07:52.753 回答