ListView OnClickListener不会启动活动

时间:2013-06-26 09:20:03

标签: android listview onclicklistener

我在我的代码中有一个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 个答案:

答案 0 :(得分:0)

您设置了不可点击的

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

你试图点击某个项目,它无效。

答案 1 :(得分:0)

你犯了两个错误:

  1. list.setClickable(false);
  2. Intent in = new Intent(getActivity(), ImageGridActivity.class);
  3. 更改以下两行:

    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);
    

答案 2 :(得分:0)

试试这个

Intent in = new Intent(getActivity().getApplicationContext(), ImageGridActivity.class);

答案 3 :(得分: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");
    }