列表视图在调用setOnItemClickListener方法时抛出异常

时间:2014-01-03 12:35:19

标签: android android-fragments

我在片段中调用列表视图,如果我注释代码lview.setOnItemClickListener,它正确显示结果。否则它会抛出异常01-03 17:51:38.381:E/AndroidRuntime(3578):at com.example.listviewtest.Committees.onCreateView(Committees.java:90)。代码出了什么问题?

Committees .java
================

package com.example.listviewtest;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Committees extends Fragment {

    ListView list;
    String[] web = { "Test", "Test", "Test", "Test", "Test", "Test", "Test" };
    Integer[] arrowImage = { R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4, R.drawable.image5,
            R.drawable.image6, R.drawable.image7 };
    com.example.listviewtest listAdapter;
    ListView lView;

    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    int Position;
    int FirstTime = 1;
    // final Context context = this;
    private ListAdapter listAdapter1;
    private ListView lview;
    View convertView = null;
    int web1 = 1;
    String Locationname = "Test";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // View view = inflater.inflate(R.layout.outagereport, container,
        // false);

        View view = inflater.inflate(R.layout.committees, container, false);

        listAdapter = new FedStateCommList(getActivity(), arrowImage.length,
                arrowImage, web);
        lView = (ListView) view.findViewById(R.id.FedStateComm);
        lView.setAdapter(listAdapter);
        lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                Toast.makeText(getActivity(), "Inside Item Clicked",
                        Toast.LENGTH_SHORT).show();

            }

        });
        return view;

    }   
}

committees.xml     ===============

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


 <RelativeLayout 
            android:id="@+id/FedStateCommitte"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#04B486"
            android:scaleType="fitXY" >

         <ListView
             android:id="@+id/FedStateComm"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"
             android:gravity="center"
             android:layout_marginLeft="0dp"
             android:divider="#ffffff"
             android:dividerHeight="4dp"
              >
         </ListView>


    </RelativeLayout>

 </FrameLayout>

federalstatecommitteelist.xml  ==============================

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:clickable="false"

    android:focusable="false"
    android:focusableInTouchMode="false">


    <ImageButton
        android:id="@+id/FedState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:descendantFocusability="blocksDescendants" />

    <TextView
        android:id="@+id/fedStateCom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Medium Text"
        android:textColor="#ffffff" 
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

</RelativeLayout>




FedStateCommList.java
=====================

package com.example.listviewtest;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;



public class FedStateCommList extends ArrayAdapter<String> {

    private final Activity context;
    private final int web;
    private final Integer[] arrowImage;
    private final String [] Title;

    ImageButton imageButton;

    //private final Integer[] imageId;
    public FedStateCommList(Activity context,
    int web,Integer [] arrowImage,String [] Title) {
    super(context,web);
    this.context = context;
    this.web = web;
    this.arrowImage = arrowImage;
    this.Title = Title;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View convertView= inflater.inflate(R.layout.federalstatecommitteelist, parent, false);

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.fedStateCom);
        ImageButton arrowimage = (ImageButton) convertView.findViewById(R.id.FedState);

        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setTextColor(Color.WHITE);
        lblListHeader.setText(Title[position]);
        arrowimage.setImageResource(arrowImage[position]);
        arrowimage.setFocusable(false);

        return convertView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web;
    }
    @Override
    public boolean isEnabled(int position)
    {
    return true;
    }
}

我只使用了它。但我仍然得到例外和细节......

01-03 18:52:07.012: E/AndroidRuntime(3966): FATAL EXCEPTION: main
01-03 18:52:07.012: E/AndroidRuntime(3966): java.lang.NullPointerException
01-03 18:52:07.012: E/AndroidRuntime(3966):     at com.example.listviewtest.Committees.onCreateView(Committees.java:90)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.os.Handler.handleCallback(Handler.java:605)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.os.Looper.loop(Looper.java:137)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 18:52:07.012: E/AndroidRuntime(3966):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案