简体   繁体   中英

ListView OnClickListener doesnt start activity

i have in my code a ListView and when i try to click on item i have a NullPointerException on (*)<-- see the code:

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

}

}

and this is the code 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>

the 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>

What can i do? i didnt find solution on internet... anyone help :D

You set non clickable

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

And you are trying to click on an item, it can't works.

You have done two mistakes:

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

Change these two lines as below:

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

尝试这个

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

You need to Override onListItemClick .

Declare menuItems as a class Variable.

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

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

 }

Note : keys must match

To retrieve in ImageGridActivity .

    Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
        String value1 = extras.getString("key1");
        String value2 = extras.getString("key2");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM