无法在DetailActivity中呈现ListView的下一项 [英] Cannot render the next item of the ListView in DetailActivity

查看:81
本文介绍了无法在DetailActivity中呈现ListView的下一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,正在尝试制作一个简单的字典.因此,当用户单击单词(列表项)时,将使用DetailActivity打开详细信息(单词和定义).问题是,当单击Next按钮时,我无法使DetailActivity下一个单词刷新.

I'm very new to android and trying to make a simple dictionary. So that when user clicks on a word (list item), the details (word+definition) opens using a DetailActivity. The problem is that I can not make the DetailActivity to be refreshed with the next word, when the Next button is clicked.

MainActivity:

MainActivity:

public class MainActivity extends AppCompatActivity {

    EditText mEditTextWord;
    EditText mEditTextDefinition;
    DictionaryDatabase mDB;
    ListView mListView;


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


        mDB = new DictionaryDatabase(this);
        mEditTextWord = (EditText)findViewById(R.id.editTextWord);
        mEditTextDefinition = (EditText)findViewById(R.id.editTextDefinition);
        Button buttonAddUpdate = (Button)findViewById(R.id.buttonAddUpdate);

        buttonAddUpdate.setOnClickListener(
                new  View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        saveRecord();
                    }
                });


        mListView = (ListView)findViewById(R.id.listView);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View
                    view, int position, long id) {
                String nextId = String.valueOf(id+1);
                Intent intent = new Intent(view.getContext(),DetailActivity.class);
                intent.putExtra("key" ,mDB.getWord(id)+"");
                intent.putExtra("value",mDB.getDefinition(id)+"");
                intent.putExtra("nextId",nextId+"");
                startActivity(intent);
            }
        });

        mListView.setOnItemLongClickListener(new  AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,
                        "Records deleted = " + mDB.deleteRecord(id),
                        Toast.LENGTH_SHORT).show();

                updateWordList();
                return true;
            }
        });
        updateWordList();

    }
...

和DetailActivity

and DetailActivity

public class DetailActivity extends AppCompatActivity {

    Button shareBtn ;
    Button nextBtn ;
    DictionaryDatabase mDB;
    TextView title, body; //Globally Declaration
    String nextId;
    int id;

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

        try{
            String key = getIntent().getStringExtra("key");
            String value = getIntent().getStringExtra("value");
            String idString = getIntent().getStringExtra("nextId");
            id = Integer.parseInt(idString) + 1;  //to be used to render the next item


            title = (TextView) findViewById(R.id.title);
            title.setText(key);

            body = (TextView) findViewById(R.id.body);
            body.setText(value);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        //render the next item. There is no error without this part
        mDB = new DictionaryDatabase(this);
        nextBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String key = mDB.getWord(id);
                String value = mDB.getDefinition(id);

                title = (TextView) findViewById(R.id.title);
                title.setText(key);

                body = (TextView) findViewById(R.id.body);
                body.setText(value);
            }
        });    
    }
}

运行时错误:

com.example.dict1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.example.dict1, PID: 22547
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dict1/com.example.dict1.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我几个小时都在此库存,因此,感谢您提出的解决问题的提示.

I'm stock on this for hours, so appreciate your hints to resolve it.

更新:详细信息页面的布局:

Update: layout of the detail page:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.dictshop.dict1.DetailActivity"
    tools:showIn="@layout/activity_detail">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="txtBody"
        android:id="@+id/body"
        android:layout_below="@+id/title"
        android:layout_marginTop="55dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:id="@+id/shareBtn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"
        android:onClick="shareThis"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next"
        android:id="@+id/nextBtn"
        android:layout_alignTop="@+id/shareBtn"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="nextWord"
        />
</RelativeLayout>

主页布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dictshop.dict1.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

和DictionaryDatabse.java

And DictionaryDatabse.java

package com.dict1shop.dict1;


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

/**
 * Created by me on 19/05/16.
 */
public class DictionaryDatabase extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "dict1.db";
    private static final String TABLE_DICTIONARY = "dictionary";
    private static final String FIELD_WORD = "word";
    private static final String FIELD_DEFINITION = "definition";
    private static final int DATABASE_VERSION = 1;


    DictionaryDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_DICTIONARY +
                "(_id integer PRIMARY KEY," +
                FIELD_WORD + " TEXT, " +
                FIELD_DEFINITION + " TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Handle database upgrade as needed
    }

    public void saveRecord(String word, String definition) {
        long id = findWordID(word);
        if (id>0) {
            updateRecord(id, word,definition);
        } else {
            addRecord(word,definition);
        }

    }

    public long addRecord(String word, String definition) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FIELD_WORD, word);
        values.put(FIELD_DEFINITION, definition);
        return db.insert(TABLE_DICTIONARY, null, values);
    }
    public int updateRecord(long id, String word, String definition) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("_id", id);
        values.put(FIELD_WORD, word);
        values.put(FIELD_DEFINITION, definition);
        return db.update(TABLE_DICTIONARY, values, "_id = ?",
                new String[]{String.valueOf(id)});
    }
    public int deleteRecord(long id) {
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(TABLE_DICTIONARY, "_id = ?", new
                String[]{String.valueOf(id)});
    }

    public long findWordID(String word) {
        long returnVal = -1;
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "SELECT _id FROM " + TABLE_DICTIONARY +
                        " WHERE " + FIELD_WORD + " = ?", new String[]{word});
        Log.i("findWordID","getCount()="+cursor.getCount());
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            returnVal = cursor.getInt(0);
        }
        return returnVal;
    }


    public String getWord(long id) {
        String returnVal = "";
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "SELECT word FROM " + TABLE_DICTIONARY +
                        " WHERE _id = ?", new String[]{String.valueOf(id)});
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            returnVal = cursor.getString(0);
        }
        return returnVal;
    }


    public String getDefinition(long id) {
        String returnVal = "";
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(
                "SELECT definition FROM " + TABLE_DICTIONARY +
                        " WHERE _id = ?", new String[]{String.valueOf(id)});
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            returnVal = cursor.getString(0);
        }
        return returnVal;
    }


    public Cursor getWordList() {
        SQLiteDatabase db = getReadableDatabase();
        String query = "SELECT _id, " + FIELD_WORD +
                " FROM " + TABLE_DICTIONARY + " ORDER BY " + FIELD_WORD +
                " ASC";
        return db.rawQuery(query, null);
    }
}

推荐答案

获得NullPointerException的原因是您从未初始化Button nextBtn.您已声明它,但未初始化它,因此对象引用现在为null.在onClickListener()之前,请尝试以下操作:

The reason you're getting a NullPointerException is that you never initialized your Button nextBtn. You declared it but you did not initialize it so the object reference is null right now. Before the onClickListener() try the following:

nextBtn = (Button) findViewById(R.id.id_of_next_Btn);

如果您还有其他问题,请在评论中告诉我.

Let me know in the comments if you have any more questions.

这篇关于无法在DetailActivity中呈现ListView的下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆