Android,App Crashing,ListView监听器

时间:2013-07-26 23:07:52

标签: android android-listview

我需要帮助找出此代码中的错误,我已经搜索了如何实现ListView OnItemClickListeners,但我无法让它们工作。

应用程序运行正常,没有设置OnItemClickListeners的方法,当我设置它们时,应用程序崩溃。

此活动的目的是使用与微调器对应的客户端加载列表(微调器包含不同的城市),然后每当单击某个项目时,启动其他活动。

public class MainActivity extends Activity {

    private Spinner spinner2;
    private Button sync;
    private static String selectedOption = "";
    private ListView listview;
    private static ArrayList<String> values;
    private static Client[] clientArray;

    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sync = (Button) findViewById(R.id.bSync);

        //Client list
        clientArray = new Client[5];

        //Populate the client list
        clientArray[0] = new Client("Roberto Herrero", "Resistencia", "Calle Falsa 123", 10.0f);
        clientArray[1] = new Client("Carlos Stigeslts", "Resistencia", "Calle Falsa 123", 10.0f);
        clientArray[2] = new Client("Pedro Zaragoza", "Resistencia", "Calle Falsa 123", 10.0f);
        clientArray[3] = new Client("Pablo Pedroso", "Charata", "Calle Falsa 123", 10.0f);
        clientArray[4] = new Client("Juan Jose Falalalala", "Resistencia", "Calle Falsa 123", 10.0f);

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();

        int count = 0;

        if(clientArray != null && clientArray.length != 0) {
            String local = clientArray[0].getLocality();
            list.add(local);
            for(int i = 0; i < clientArray.length; i++) {
                if(!local.equals(clientArray[i].getLocality())) {
                    if(!list.contains(clientArray[i].getLocality())) {
                        list.add(clientArray[i].getLocality());
                    }
                }
            }
        }
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);

        addSpinnerListener();
        addButtonListeners();

        //THIS IS CAUSING THE APP TO CRASH, WITHOUT THIS METHOD CALL, THE APPS RUNS FINE
        setListviewListeners();

    }

    public void setListviewListeners() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent("android.intent.action.CLIENTVIEW");
                startActivity(intent);
            }
        });
    }

    public void addSpinnerListener() {
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
                String selected= parent.getItemAtPosition(position).toString();
                selectedOption = selected;

                //List Values
                values = new ArrayList<String>();

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

                    if(clientArray[i].getLocality().equalsIgnoreCase(selectedOption)) {
                        values.add(clientArray[i].getName() + "\n" + clientArray[i].getAddress());
                    }
                }

                //Declare, Define and find the ListView
                listview = (ListView) findViewById(R.id.listview);

                ArrayList<String> vlist = new ArrayList<String>();
                for (int i = 0; i < values.size(); ++i) {
                    vlist.add(values.get(i));
                }

                ArrayAdapter adapter = new ArrayAdapter(context,
                        android.R.layout.simple_list_item_1, vlist);

                listview.setAdapter(adapter);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    public void addButtonListeners() {
        sync.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                // set title
                alertDialogBuilder.setTitle("REPORT");

                // set dialog message
                alertDialogBuilder.setMessage("NOT IMPLEMENTED");
                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }
        });
    }
}

这是xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <Button
            android:id="@+id/bSync"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SINCRONIZAR" />

    <Spinner
            android:id="@+id/spinner2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/listview"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>

</LinearLayout>

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sistel.listview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sistel.listview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
                android:name="com.sistel.listview.ClientView"
                android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.CLIENTVIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

这是我想要开始的活动

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">



    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:weightSum="100">

        <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dip">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="80"
                    android:text="Nombre"
                    android:id="@+id/textView"
                    android:layout_column="0"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="15"
                    android:text="Saldo"
                    android:id="@+id/textView2"
                    android:layout_column="1"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:text="Cascos"
                    android:id="@+id/textView3"
                    android:layout_column="2"/>
        </TableRow>

    <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="80"
                android:text="Saldo Anterior"
                android:id="@+id/textView"
                android:layout_column="0"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="15"
                android:text="SaldoA"
                android:id="@+id/textView2"
                android:layout_column="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="CascosA"
                android:id="@+id/textView3"
                android:layout_column="2"/>
    </TableRow>

    <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="80"
                android:text="Retiros"
                android:id="@+id/textView"
                android:layout_column="0"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="15"
                android:text="SaldoA"
                android:id="@+id/textView2"
                android:layout_column="1"/>

        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:inputType="number"
                android:ems="2"
                android:id="@+id/editText"/>
    </TableRow>

    <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="80"
                android:text="Entrega"
                android:id="@+id/textView"
                android:layout_column="0"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="15"
                android:text="Entrega"
                android:id="@+id/textView2"
                android:layout_column="1"/>

        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:inputType="number"
                android:ems="2"
                android:id="@+id/editText"/>
    </TableRow>

    <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="80"
                android:text="Pagos"
                android:id="@+id/textView"
                android:layout_column="0"/>

        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="15"
                android:inputType="number"
                android:ems="2"
                android:id="@+id/editText"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text=""
                android:id="@+id/textView2"
                android:layout_column="1"/>
    </TableRow>

    <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="80"
                android:text="Pagos"
                android:id="@+id/textView"
                android:layout_column="0"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="15"
                android:text="SaldoAc"
                android:id="@+id/textView2"
                android:layout_column="1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="CascosAc"
                android:id="@+id/textView2"
                android:layout_column="1"/>
    </TableRow>

这是我想要开始的活动的JAVA

public class ClientView extends Activity {

    private Button update;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);

        initComponets();
        buttonListener();

    }

    public void initComponets() {
        update = (Button) findViewById(R.id.bUpdate);
    }

    public void buttonListener() {
        update.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

                // set title
                alertDialogBuilder.setTitle("REPORT");

                // set dialog message
                alertDialogBuilder.setMessage("DUMMY PROCEDURE");
                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }
        });

    }

}

1 个答案:

答案 0 :(得分:2)

如果我错了,请纠正我但在我看来你永远不会初始化listview。你在这里声明

private ListView listview;

但似乎没有初始化它,至少在尝试设置listener之前没有。如果这不是您的问题,那么请发布logcat,以便我们可以确切地看到问题所在。