Android listview - 在第二个活动中阻止

时间:2014-12-10 15:57:45

标签: java android android-listview

我们已经制作了一个分组发送短信的应用程序,但我们的应用程序在第二个活动中崩溃了...... 如果有人可以帮助我们,我们会很开心! :)

这是代码:

MainActivity.java

*package fr.deust.androidproject;
**import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
public class MainActivity extends Activity implements
    android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv;
ArrayList<Repertoire> repertoireList;
ArrayList<Repertoire> repertoireListSelectect;
RepertoireAdapter rpAdapter;
private void displayRepertoireList() {
    repertoireList = new ArrayList<Repertoire>();
    repertoireList.add(new Repertoire("Richard NGU LEUBOU", "+33650026569"));
    repertoireList.add(new Repertoire("Yohan Peloille", "+33642526569"));
    repertoireList.add(new Repertoire("Colin Garisson", "+336470265691"));
    repertoireList.add(new Repertoire("Clara Elefant", "+33647026570"));
    repertoireList.add(new Repertoire("William Henckel", "+33657026567"));
    repertoireList.add(new Repertoire("Richard NGU LEUBOU", "+33697026563"));
    repertoireList.add(new Repertoire("Yohan Peloille", "+33647026564"));
    repertoireList.add(new Repertoire("Colin Garisson", "+33647086561"));
    repertoireList.add(new Repertoire("Clara Elefant", "+33647026522"));
    repertoireList.add(new Repertoire("William Henckel", "+33647026555"));

    rpAdapter = new RepertoireAdapter(repertoireList, this);
    lv.setAdapter(rpAdapter);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int pos = lv.getPositionForView(buttonView);
        Repertoire p = repertoireList.get(pos);
        p.setSelected(isChecked);
        if (isChecked==true){
            repertoireListSelectect.add(p); 
        }
        else{
            repertoireListSelectect.remove(p);
        }



}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    repertoireListSelectect = new ArrayList<Repertoire>();
    lv = (ListView) findViewById(R.id.listview);
    displayRepertoireList();
    final Button Button_validation = (Button) findViewById(R.id.button_validation1);
    Button_validation.setOnClickListener(new OnClickListener(){
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
         intent.putExtra("selectedContactsList", repertoireListSelectect);
         startActivity(intent);
     }
    });
        final Button buttonSelectAll=(Button) findViewById(R.id.button_select_all);
        buttonSelectAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final CheckBox checkbox= (CheckBox) findViewById(R.id.chk_box);
                boolean checked = true;
                checkbox.setChecked(checked);
            }
        }); 

  }


}***

DisplayActivity.java

package fr.deust.androidproject;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayActivity extends Activity {
    @SuppressWarnings("unchecked")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        TextView tv=(TextView) findViewById(R.id.textView1);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        ArrayList<Repertoire> contactList = new ArrayList<Repertoire>();
        contactList = (ArrayList<Repertoire>) extras.get("selectedContactsList");
        for (int i=0; i<contactList.size(); i++){
            Repertoire a = contactList.get (i);
           tv.setText (a.getName ());
        }

        final Button validationButton = (Button) findViewById(R.id.button1);
        validationButton.setOnClickListener(new OnClickListener(){

         @Override
         public void onClick(View v) {
          Toast.makeText(getApplicationContext(), "Vous avez validé votre action", Toast.LENGTH_LONG).show(); 
          Intent intent = new Intent(DisplayActivity.this, MainActivity.class);
          startActivity(intent);
         }
        });
        final Button AnullerButton = (Button) findViewById(R.id.button2);
        AnullerButton.setOnClickListener(new OnClickListener(){

         @Override
         public void onClick(View v) { 
          Intent intent = new Intent(DisplayActivity.this, MainActivity.class);
          startActivity(intent);
         }
        });
    }


}

RepertoireAdapter.java

package fr.deust.androidproject;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

class Repertoire {

    String name;
    String telephone;
    boolean selected = false;

    public Repertoire(String name, String telephone) {
        super();
        this.name = name;
        this.telephone = telephone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone= telephone;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

public class RepertoireAdapter extends ArrayAdapter<Repertoire>{

    private List<Repertoire> repertoireList;
    private Context context;

    public RepertoireAdapter(List<Repertoire> repertoireList, Context context) {
        super(context, R.layout.single_list_view, repertoireList);
        this.repertoireList = repertoireList;
        this.context = context;
    }

    private static class RepertoireHolder {
        public TextView repertoireName;
        public CheckBox chkBox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        RepertoireHolder holder = new RepertoireHolder();

        if(convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.single_list_view, null);
            holder.repertoireName = (TextView) v.findViewById(R.id.name);
            holder.chkBox = (CheckBox) v.findViewById(R.id.chk_box);

            holder.chkBox.setOnCheckedChangeListener((MainActivity) context);

        } else {
            holder = (RepertoireHolder) v.getTag();
        }

        Repertoire p = repertoireList.get(position);
        holder.repertoireName.setText(p.getName());
        holder.chkBox.setChecked(p.isSelected());
        holder.chkBox.setTag(p);

        return v;
    }
}

我们添加了这几行,但总是错误的:

MainActivity.java

第47行

 if (isChecked==true){
            repertoireListSelectect.add(p);    
        }
        else{
            repertoireListSelectect.remove(p);
        }

第71行

Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
         intent.putExtra("selectedContactsList", repertoireListSelectect);
         startActivity(intent);

DisplayActivity.java

第20行

   Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        ArrayList<Repertoire> contactList = new ArrayList<Repertoire>();
        contactList = (ArrayList<Repertoire>) extras.get("selectedContactsList");
        for (int i=0; i<contactList.size(); i++){
            Repertoire a = contactList.get (i);
           tv.setText (a.getName ());
        }

这是logcat错误:

蓝色和橙色:

12-10 19:58:32.257: D/gralloc_goldfish(568): Emulator without GPU emulation detected.

12-10 19:58:32.327: W/TextLayoutCache(568): computeValuesWithHarfbuzz -- need to force to single run

12-10 19:58:42.727: D/AndroidRuntime(568): Shutting down VM

12-10 19:58:42.747: W/dalvikvm(568): threadid=1: thread exiting with uncaught exception (group=0x409961f8)

红色:

12-10 19:58:42.797: E/AndroidRuntime(568): FATAL EXCEPTION: main

12-10 19:58:42.797: E/AndroidRuntime(568): java.lang.NullPointerException

12-10 19:58:42.797: E/AndroidRuntime(568): at fr.deust.androidproject.MainActivity.onCheckedChanged(MainActivity.java:52)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.widget.CompoundButton.toggle(CompoundButton.java:87)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.widget.CompoundButton.performClick(CompoundButton.java:99)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.view.View$PerformClick.run(View.java:13983)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.os.Handler.handleCallback(Handler.java:605)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.os.Handler.dispatchMessage(Handler.java:92)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.os.Looper.loop(Looper.java:137)

12-10 19:58:42.797: E/AndroidRuntime(568): at android.app.ActivityThread.main(ActivityThread.java:4340)

12-10 19:58:42.797: E/AndroidRuntime(568): at java.lang.reflect.Method.invokeNative(Native Method)

12-10 19:58:42.797: E/AndroidRuntime(568): at java.lang.reflect.Method.invoke(Method.java:511)

12-10 19:58:42.797: E/AndroidRuntime(568): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

12-10 19:58:42.797: E/AndroidRuntime(568): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

12-10 19:58:42.797: E/AndroidRuntime(568): at dalvik.system.NativeStart.main(Native Method)

最后是绿色:

12-10 20:03:43.227: I/Process(568): Sending signal. PID: 568 SIG: 9

非常感谢你!希望有人可以帮助我们!! :)

1 个答案:

答案 0 :(得分:0)

您的适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    Repertoire p;
    RepertoireHolder holder = new RepertoireHolder();

    if(convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.single_list_view, null);
        holder.repertoireName = (TextView) v.findViewById(R.id.name);
        holder.chkBox = (CheckBox) v.findViewById(R.id.chk_box);

        holder.chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            // TODO Auto-generated method stub
            p = repertoireList.get(position);
            p.setSelected(arg1);
            if (arg1){
               repertoireListSelectect.add(p); 
            }
            else{
             repertoireListSelectect.remove(p); // might wona put a try catch block here
            }
        }
    });

    } else {
        holder = (RepertoireHolder) v.getTag();
    }        
    holder.repertoireName.setText(p.getName());
    holder.chkBox.setChecked(p.isSelected());
    holder.chkBox.setTag(p);

    return v;
 }
}

尝试使用适配器并从活动中删除checkChangeListener