在适配器类上获取null异常

时间:2014-03-19 11:45:24

标签: android listview android-listview

我创建了一个应用程序,其中1个在数据库中有2个表,在app中有2个listview,在listview 1中显示table1,在listview2上显示表2 ..但是在listview1上运行app得到null异常。 setAdapter(适配器1);在第二个列表上没有错误..,我的适配器类有什么问题。我为listview创建了相同的适配器类。

Campaign_details

package com.example.smscampaign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Semaphore;




import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Campaign_Details extends Activity implements OnItemLongClickListener {
    Cursor c;
    private int ACTIVITY_EDIT=1;
     SQLiteDatabase db;
     String str,str1,str2;
    private ListView lvMessage;
    private Demo adapter;
    private Demo1 adapter1;
    public String ADD_TO_NAME = "toname";
    ListView listview ,listview1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_demostration);
        TextView txt1 = (TextView) findViewById(R.id.data);
        listview = (ListView) findViewById(R.id.listview);
        listview1 = (ListView) findViewById(R.id.listView1);
        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        txt1.setText(pInfo.versionName);

        TextView txt = (TextView) findViewById(R.id.textnum1);
        TextView txt2 = (TextView) findViewById(R.id.textnum2);

        // String [] values1= data.split("\n");
        // int t = values1.length;
        // txt.setText(Integer.toString(t));

    final DatabaseHelp info = new DatabaseHelp(this);
        info.open();
        ArrayList<String> arr1 = info.getData1();
        final ArrayList<String> list1 = new ArrayList<String>();
        for (int i = 0; i < arr1.size(); i++) {
            list1.add(arr1.get(i));
        }
        int t1 = arr1.size();
        txt2.setText(Integer.toString(t1));

        adapter1 = new Demo1(this, list1);
        adapter1.setNotifyOnChange(true);

        listview1.setAdapter(adapter1);

        listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {

                TextView tv = (TextView) view.findViewById(R.id.text1);
                String value = tv.getText().toString();
                c=info.fetchChildren1(value);
                if (c.moveToFirst()) 
                str = (c.getString(c.getColumnIndex("name"))); 
                str1 = (c.getString(c.getColumnIndex("message"))); 
                str2 = (c.getString(c.getColumnIndex("Contacts"))); 
                    c.close();
                Intent intent = new Intent(Campaign_Details.this, SmsSend.class);
                 intent.putExtra("key",str);
                 intent.putExtra("key1",str1);
                 intent.putExtra("key2",str2);
                startActivity(intent);


            }

        });
        listview1.setOnItemLongClickListener(this);
        ArrayList<String> arr = info.getData();

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < arr.size(); i++) {
            list.add(arr.get(i));
        }
        int t = arr.size();
        txt.setText(Integer.toString(t));
        adapter = new Demo(this, list);
        adapter.setNotifyOnChange(true);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {

                TextView tv = (TextView) view.findViewById(R.id.text1);
                String value = tv.getText().toString();
                c=info.fetchChildren(value);
                if (c.moveToFirst()) 
                str = (c.getString(c.getColumnIndex("person_name"))); 
                str1 = (c.getString(c.getColumnIndex("scale_person"))); 
                str2 = (c.getString(c.getColumnIndex("Contacts_person"))); 
                    c.close();
                Intent intent = new Intent(Campaign_Details.this, SmsSend.class);
                 intent.putExtra("key",str);
                 intent.putExtra("key1",str1);
                 intent.putExtra("key2",str2);
                startActivity(intent);


            }

        });
        listview.setOnItemLongClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.main2, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.nextPage:
            Intent i = new Intent(Campaign_Details.this, SmsSend.class);
            startActivity(i);
            break;

        }
        return true;
    }

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


        return false;
    }

}

Demo1.java

  package com.example.smscampaign;

    import java.util.ArrayList;


    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class Demo1 extends ArrayAdapter<String>{

    private ArrayList<String> values1=null;
    private final Context context;
    public Demo1(Context context, ArrayList<String> values1) {
        super(context,R.layout.list, values1);
        this.context=context;
        this.values1= values1;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowview= inflater.inflate(R.layout.list, parent,false);
        TextView txt=(TextView) rowview.findViewById (R.id.text1);
        //ImageView img=(ImageView) rowview.findViewById (R.id.icon);
        txt.setText(values1.get(position));
        //changing icon

        return rowview;
    }
    }





Demo.java



package com.example.smscampaign;

import java.util.ArrayList;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Demo extends ArrayAdapter<String>{

private ArrayList<String> values=null;
private final Context context;
public Demo(Context context, ArrayList<String> values) {
    super(context,R.layout.list, values);
    this.context=context;
    this.values= values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowview= inflater.inflate(R.layout.list, parent,false);
    TextView txt=(TextView) rowview.findViewById (R.id.text1);
    //ImageView img=(ImageView) rowview.findViewById (R.id.icon);
    txt.setText(values.get(position));
    //changing icon

    return rowview;
}
}

1 个答案:

答案 0 :(得分:0)

可能listview1未正确初始化,导致以下行中出现NullPointerException ...

listview1.setAdapter(adapter1);

另一件事,从此行中删除final修饰符...

private final Context context;

如果使用final修饰符声明对象,则无法更改该对象的值。