我的应用程序崩溃listView.setOnItemClickListener(this)

时间:2016-08-10 20:11:53

标签: android listview

当我更改为listView.setOnItemClickListener(this)listView.setAdapter(listDataAdapter);的活动时,它开始崩溃。我不知道为什么会这样,请告诉我哪里出错了< /em>。在logcat中它指向listView.setAdapter(listDataAdapter);

DataListActivity

 package ie.example.artur.projectrepeat;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DataListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DatabaseClass  database;
    Cursor cursor;
    ListDataAdapter listDataAdapter;


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


        listView = (ListView) findViewById(R.id.list_view);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        listView.setOnItemClickListener(this);
        database = new DatabaseClass(getApplicationContext());

        sqLiteDatabase = database.getReadableDatabase();



        if (cursor.moveToFirst()) {
            do {

                String id, product_name, category;
                id = cursor.getString(0);
                product_name = cursor.getString(1);
                category = cursor.getString(2);
                DataProvider dataProvider = new DataProvider(id, product_name, category);
                listDataAdapter.add(dataProvider);
            } while (cursor.moveToNext());
        }
    }

   @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv = (TextView) view.findViewById(R.id.product_id);
        sqLiteDatabase = database.getReadableDatabase();
        DatabaseClass.DeleteInformation(tv.getText().toString(), sqLiteDatabase);


        listDataAdapter.notifyDataSetChanged();


    }


    }

数据库类

package ie.example.artur.projectrepeat;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;

/**
 * Created by family on 06/07/2016.
 */
public class DatabaseClass extends SQLiteOpenHelper {
    public static final String DATABASE_Name = "Product.db2";
    public static final String Table_Name = "product_table2";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "Name";
    public static final String COL_3 = "Quantity";
    public static final String COL_4 = "Category";
    public static final String COL_5 = "Importance";
    public DatabaseClass(Context context) {
        super(context, DATABASE_Name, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + Table_Name + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,Quantity TEXT,Category INTEGER,Importance TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("Drop Table If Exists" + Table_Name);
        onCreate(db);
    }

    public boolean insertData(String name, String quantity, String category,String importance) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, quantity);
        contentValues.put(COL_4, category);
        contentValues.put(COL_5, importance);

        long result = db.insert(Table_Name, null, contentValues);

            if (result == -1)
                return false;
            else
                return true;
        }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + Table_Name, null);
        return res;
    }

    public boolean updateData(String id,String name,String quantity,String category,String importance ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, id);
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, quantity);
        contentValues.put(COL_4, category);
        contentValues.put(COL_5, importance);
        db.update(Table_Name,contentValues,"id =?",new String[]{id});
                return true;
    }

   /* public Cursor getCursor(){
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        queryBuilder.setR
    }
*/
   public Integer DeleteData (String id) {
       SQLiteDatabase db = this.getWritableDatabase();
       return db.delete(Table_Name,"ID = ?",new String[]{id});
   }
    public static void DeleteInformation(String item_name, SQLiteDatabase sqLiteDatabase){

        String selection = COL_1+" LIKE ?";
        String [] selection_args = {item_name};
        sqLiteDatabase.delete(Table_Name,selection,selection_args);

    }

    public Cursor getInformation(SQLiteDatabase sqLiteDatabase)
    {
        Cursor cursor;
        String [] Projections = {COL_1,COL_2,COL_4};

        cursor  = sqLiteDatabase.query(Table_Name,Projections,null,null,null,null,null);
        return cursor;

    }

    public Cursor getItem(String item_name ,SQLiteDatabase sqLiteDatabase){
        String [] Projections = {COL_1,COL_2,COL_3,COL_4,COL_5};
        String selection = COL_1+" LIKE ?";
        String [] selection_args = {item_name};
        Cursor cursor = sqLiteDatabase.query(Table_Name,Projections,selection,selection_args,null,null,null);
        return cursor;

    }


}

的DataProvider

*package ie.example.artur.projectrepeat;
/**
 * Created by family on 10/08/2016.
 */
public class DataProvider {
    private String id;
    private String product_name;
    private String category;
    public DataProvider(String id,String product_name,String category)
    {
    this.id = id;
    this.product_name=product_name;
    this.category=category;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getProduct_name() {
        return product_name;
    }
    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }
}*

ListDataAdapter

package ie.example.artur.projectrepeat;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by family on 10/08/2016.
 */
public class ListDataAdapter extends ArrayAdapter  {
   List list = new ArrayList ();

    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }





    static class LayoutHandler
    {
        TextView id,product_name,category;

    }
    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

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

    }



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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       LayoutHandler layoutHandler;
        View row = convertView;
        if(row == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.id = (TextView) row.findViewById(R.id.product_id);
            layoutHandler.product_name = (TextView) row.findViewById(R.id.product_name);
            layoutHandler.category = (TextView) row.findViewById(R.id.product_category);
            row.setTag(layoutHandler);
        }
        else {
            layoutHandler = (LayoutHandler) row.getTag();

        }
        DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.id.setText(dataProvider.getId());
        layoutHandler.product_name.setText(dataProvider.getProduct_name());
        layoutHandler.category.setText(dataProvider.getCategory());



        return row;




    }


    }

这是我的logcat

*08-10 21:49:09.693 16004-16004/ie.example.artur.projectrepeat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: ie.example.artur.projectrepeat, PID: 16004
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.example.artur.projectrepeat/ie.example.artur.projectrepeat.DataListActivity}: java.lang.NullPointerException
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:161)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:157)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5356)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                                                                                    at dalvik.system.NativeStart.main(Native Method)
                                                                                 Caused by: java.lang.NullPointerException
                                                                                    at ie.example.artur.projectrepeat.DataListActivity.onCreate(DataListActivity.java:43)
                                                                                    at android.app.Activity.performCreate(Activity.java:5426)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:161) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:157) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5356) 
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
                                                                                    at dalvik.system.NativeStart.main(Native Method)* 

2 个答案:

答案 0 :(得分:0)

您应首先初始化listViewlistDataAdapter变量,然后再使用它们。为此,稍微重新排序代码就足够了。

更改以下内容

// Here 'listView' and 'listDataAdapter' aren't initialized
listView.setAdapter(listDataAdapter);
listView.setOnItemClickListener(this);

setContentView(R.layout.data_list_layout);

// This line initializes the 'listView'
listView = (ListView) findViewById(R.id.list_view);

// And this one initializes the 'listDataAdapter'
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);

setContentView(R.layout.data_list_layout);

listView = (ListView) findViewById(R.id.list_view);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(listDataAdapter);
listView.setOnItemClickListener(this);

另请注意,findViewById后应setContentView

答案 1 :(得分:0)

由于您的human.Health = 0为空,而您在空listView上呼叫setAdapter

将您的代码更改为

首先listViewsetContent

Activity

然后从setContentView(R.layout.data_list_layout); 通过listView

找到您的XML
id

然后使用listView = (ListView) findViewById(R.id.list_view);

执行您想要的操作

listViewsetAdapter

完整代码: -

setOnItemClickListener