2016-05-21 8 views
0

gerendert werden Ich bin sehr neu in Android und versuchen, ein einfaches Wörterbuch zu machen. Wenn ein Benutzer auf ein Wort (Listenelement) klickt, werden die Details (Wort + Definition) mit einer DetailActivity geöffnet. Das Problem ist, dass ich die DetailActivity nicht mit dem nächsten Wort aktualisieren kann, wenn auf die Schaltfläche Next geklickt wird.Das nächste Element der ListView kann nicht in DetailActivity

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(); 

    } 
... 

und 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); 
      } 
     });  
    } 
} 

Laufzeitfehler:

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) 

Ich bin stundenlang auf diese Aktie, so schätzen Ihre Hinweise, es zu lösen.

Update: Layout der Detailseite:

<?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> 

Aufbau der Hauptseite:

<?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> 

Und 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); 
    } 
} 

Antwort

1

Der Grund, warum Sie eine NullPointerException erhalten, ist, dass Sie nie Ihre Button nextBtn initialisiert haben. Sie haben es deklariert, aber Sie haben es nicht initialisiert, so dass die Objektreferenz jetzt null ist. Bevor die onClickListener() versuchen Sie Folgendes:

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

Lassen Sie mich in den Kommentaren wissen, wenn Sie weitere Fragen haben.

+0

Ich bin etwas verwirrt, was Sie erreichen möchten. Können Sie die Layouts auch posten, damit ich Ihren Code auf meinem Terminal ausführen kann? –

+0

Abgesehen von Ihrem Tipp, ich muss nur "id + 1" bei nextBtn klicken. Und jetzt funktioniert es gut. Vielen Dank! – Karlom

0

Dafür sollten Sie Ihre Worte übergeben Liste zu DetailActivity.java Klasse, so dass auf Tipp des nächsten Knopfes, wissen Sie das nächste Wort, das yo Du brauchst Bedeutung.

Hier auf Tipp des Wortes senden Sie nur diese Wortbedeutung. Sie sollten Ihre arraylist und position des Artikels übergeben. So können Sie Daten aus der Liste an dieser bestimmten Position abrufen.

+0

Nun, mein Ziel ist eine neue Abfrage zu machen, basierend auf 'id + 1' jedes Mal, wenn die Schaltfläche' Next' angetippt wird. Ist das nicht ein praktikabler Ansatz? Wenn nicht, werden Sie sich freundlicherweise darauf konzentrieren, wie Sie die Arraylist an "DetailActivity" übergeben können? – Karlom

1

Ich denke, Ihre Fehler onClickListener() Methode wird die Zuordnung zu noch nicht initialisierten Taste, die nextBtn
in protected void onCreate(Bundle savedInstanceState) ist zu versuchen, so etwas wie dieses zu tun nextBtn= (Buttom) findViewById(R.id.nextBtn); oder was auch immer Sie es in Ihrem xml genannt füllen Ich möchte diese Hilfe

1

denke ich, in Ihrem DetailActivity.class

nextBtn == null 

Sie müssen so etwas tun:

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

vor

nextBtn.setOnClickListener(...);