ListView有两列 - NullPointerException

时间:2014-04-04 15:17:49

标签: android listview

我尝试按照本指南guide制作一个ListView,每行包含两个项目。我试图让它适应我的应用程序。

问题是我得到一个NullPointerException:

  

04-04 10:39:44.502:E / AndroidRuntime(6702):致命异常:主要   04-04 10:39:44.502:E / AndroidRuntime(6702):流程:   com.example.julegaveliste2,PID:6702 04-04 10:39:44.502:   E / AndroidRuntime(6702):java.lang.NullPointerException 04-04   10:39:44.502:E / AndroidRuntime(6702):at   com.example.julegaveliste2.listviewAdapter.getCount(listviewAdapter.java:32)   04-04 10:39:44.502:E / AndroidRuntime(6702):at   android.widget.ListView.setAdapter(ListView.java:480)04-04   10:39:44.502:E / AndroidRuntime(6702):at   com.example.julegaveliste2.MainActivity.visliste(MainActivity.java:63)   04-04 10:39:44.502:E / AndroidRuntime(6702):at   com.example.julegaveliste2.MainActivity $ 1.onClick(MainActivity.java:131)   04-04 10:39:44.502:E / AndroidRuntime(6702):at   android.view.View.performClick(View.java:4424)04-04 10:39:44.502:   E / AndroidRuntime(6702):at   android.view.View $ PerformClick.run(View.java:18383)04-04   10:39:44.502:E / AndroidRuntime(6702):at   android.os.Handler.handleCallback(Handler.java:733)04-04   10:39:44.502:E / AndroidRuntime(6702):at   android.os.Handler.dispatchMessage(Handler.java:95)04-04   10:39:44.502:E / AndroidRuntime(6702):at   android.os.Looper.loop(Looper.java:137)04-04 10:39:44.502:   E / AndroidRuntime(6702):at   android.app.ActivityThread.main(ActivityThread.java:4998)04-04   10:39:44.502:E / AndroidRuntime(6702):at   java.lang.reflect.Method.invokeNative(Native Method)04-04   10:39:44.502:E / AndroidRuntime(6702):at   java.lang.reflect.Method.invoke(Method.java:515)04-04 10:39:44.502:   E / AndroidRuntime(6702):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)   04-04 10:39:44.502:E / AndroidRuntime(6702):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)04-04   10:39:44.502:E / AndroidRuntime(6702):at   dalvik.system.NativeStart.main(原生方法)

这是MainActivity.java:

package com.example.julegaveliste2;
import static com.example.julegaveliste2.Constant.FIRST_COLUMN;
import static com.example.julegaveliste2.Constant.SECOND_COLUMN;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity{
    DatabaseHelper db;
    Button Les_database;
    Button Knapp_LeggTill;
    Button SlettTabell;

    private ArrayList<HashMap> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button Knapp_LeggTill = (Button)findViewById(R.id.Knapp_LeggTill);
        Button Les_database = (Button)findViewById(R.id.Les_database);
        Button SlettTabell = (Button)findViewById(R.id.SlettTabell);
        Knapp_LeggTill.setOnClickListener(myhandler);
        Les_database.setOnClickListener(myhandler);
        SlettTabell.setOnClickListener(myhandler);

    }

    public static void LagTabell(SQLiteDatabase db)
    {
        ContentValues jul = new ContentValues();
        jul.put("person", "Ida");
        jul.put("gave", "blomst");
        db.insert("julegaveliste2", "person", jul); 

        jul.put("person", "Lars");
        jul.put("gave", "film");
        db.insert("julegaveliste2", "person", jul); 


    }


    public void visliste(SQLiteDatabase db)
    {       
        final String person = "db.rawQuery('SELECT person FROM julegaveliste2', null)";
        final String gave = "db.rawQuery('SELECT gave FROM julegaveliste2', null)";


        ListView lview = (ListView) findViewById(R.id.listview);
        listviewAdapter adapter = new listviewAdapter(this, list);
        lview.setAdapter(adapter);


        list = new ArrayList<HashMap>();

        HashMap temp = new HashMap();
        temp.put(FIRST_COLUMN, person);
        temp.put(SECOND_COLUMN, gave);
        list.add(temp);



    }
    public void slett_tabell(SQLiteDatabase db)
    {
        db.delete("julegaveliste2",null, null);

        Context context = getApplicationContext();
        CharSequence text = "Tabellen julegaveliste2 er slettet";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

    public void StartAddUser()
    {
        Intent intent = new Intent(this, AddUser.class);
        startActivity(intent);
        Context context = getApplicationContext();
        Toast toast = Toast.makeText(context, "Starter AddUser", Toast.LENGTH_SHORT);
        toast.show();
    }


    View.OnClickListener myhandler = new View.OnClickListener()
    {
        public void onClick(View v)
        {
            final DatabaseHelper db = new DatabaseHelper(MainActivity.this);

             switch(v.getId()) {
             case R.id.SlettTabell:
                 slett_tabell(db.getWritableDatabase());
               break;
             case R.id.Les_database:
                 visliste(db.getWritableDatabase());
               break;
             case R.id.Knapp_LeggTill:
                 StartAddUser();
               break;
           }
        }
    };


    @Override
    protected void onDestroy()
        {
            super.onDestroy();
        }

}

这是listviewAdapter.java:

package com.example.julegaveliste2;

import static com.example.julegaveliste2.Constant.FIRST_COLUMN;
import static com.example.julegaveliste2.Constant.SECOND_COLUMN;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class listviewAdapter extends BaseAdapter
{

    public ArrayList<HashMap> list;
    Activity activity;

    public listviewAdapter(Activity activity, ArrayList<HashMap> list)
    {
        super();
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    private class ViewHolder
    {
        TextView txtFirst;
        TextView txtSecond;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.listview_row, null);
            holder = new ViewHolder();
            holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
            holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap map = list.get(position);
        String petter1 = (String) map.get(FIRST_COLUMN);
        String petter2 = (String) map.get(SECOND_COLUMN);

        holder.txtFirst.setText(petter1);
        holder.txtSecond.setText(petter2);

        return convertView;
    }



}

2 个答案:

答案 0 :(得分:1)

在函数public void visliste(SQLiteDatabase db)中有空的列表,它被分配给listadapter,导致空指针。相反它应该是:

public void visliste(SQLiteDatabase db)
    {       
        final String person = "db.rawQuery('SELECT person FROM julegaveliste2', null)";
        final String gave = "db.rawQuery('SELECT gave FROM julegaveliste2', null)";


        ListView lview = (ListView) findViewById(R.id.listview);
         list = new ArrayList<HashMap>();

        HashMap temp = new HashMap();
        temp.put(FIRST_COLUMN, person);
        temp.put(SECOND_COLUMN, gave);
        list.add(temp);

        listviewAdapter adapter = new listviewAdapter(this, list);
        lview.setAdapter(adapter);

    }

答案 1 :(得分:0)

您正在将空列表传递给适配器:

这样做:

list = new ArrayList<HashMap>();
HashMap temp = new HashMap();
temp.put(FIRST_COLUMN, person);
temp.put(SECOND_COLUMN, gave);
list.add(temp);

之前:

listviewAdapter adapter = new listviewAdapter(this, list);