2015-02-11 81 views
0

嗨,我是新來的機器人和原諒我,因爲這是我第一次開發我的學校作業的移動應用程序。我有一個錯誤,當我嘗試啓動應用程序時,我的應用程序崩潰,我的猜測是檢索是罪魁禍首。java.lang.NullPointerException,在列表視圖致命異常

我正在創建一個應該顯示一個允許用戶輸入CRUD的空列表的活動。我明白,錯誤是由於空,但我怎麼做,以便它允許顯示一個空列表?我將不勝感激任何關於如何去做這件事的幫助,謝謝!

這裏是logcat的:

02-11 18:41:53.803: W/dalvikvm(18334): threadid=1: thread exiting with uncaught exception (group=0x41763da0) 
02-11 18:41:53.803: E/AndroidRuntime(18334): FATAL EXCEPTION: main 
02-11 18:41:53.803: E/AndroidRuntime(18334): Process: com.example.sgrecipe, PID: 18334 
02-11 18:41:53.803: E/AndroidRuntime(18334): java.lang.NullPointerException 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.fetchData(FavouriteFragment.java:174) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.initControls(FavouriteFragment.java:92) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.example.sgrecipe.FavouriteFragment.onStart(FavouriteFragment.java:50) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.Fragment.performStart(Fragment.java:1484) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.View.measure(View.java:17396) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer.doCallbacks(Choreographer.java:603) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer.doFrame(Choreographer.java:573) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Handler.handleCallback(Handler.java:733) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Handler.dispatchMessage(Handler.java:95) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.os.Looper.loop(Looper.java:157) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at android.app.ActivityThread.main(ActivityThread.java:5335) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at java.lang.reflect.Method.invokeNative(Native Method) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at java.lang.reflect.Method.invoke(Method.java:515) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
02-11 18:41:53.803: E/AndroidRuntime(18334): at dalvik.system.NativeStart.main(Native Method) 

FavouriteFragment.java:

package com.example.sgrecipe; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.content.ContentValues; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.Toast; 

public class FavouriteFragment extends Fragment implements OnClickListener{ 

// Primitive Variables 
String selected_ID = ""; 

// Widget GUI Declare 
EditText txtTitle, txtDesc, txtSalary; 
Button btnAdd, btnUpdate, btnDelete; 
ListView lvNotes; 

// DB Objects 
DBHelper helper; 
SQLiteDatabase db; 

// Adapter Object 
SimpleCursorAdapter adapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.activity_favourite_fragment, container, false); 

    return rootView; 

} 

    @Override 
    public void onStart() { 
    super.onStart(); 
    initControls(); 
    // Init DB Objects 
    helper = new DBHelper(getActivity());} 

    // Widget GUI Init 
    private void initControls(){ 
    txtTitle = (EditText)getView().findViewById(R.id.txtTitle); 
    txtDesc = (EditText)getView().findViewById(R.id.txtDes); 
    txtSalary = (EditText)getView().findViewById(R.id.txtSalary); 
    lvNotes = (ListView)getView().findViewById(R.id.lvNotes); 

    btnAdd = (Button)getView().findViewById(R.id.btnAdd); 
    btnUpdate = (Button)getView().findViewById(R.id.btnUpdate); 
    btnDelete = (Button)getView().findViewById(R.id.btnDelete); 

    // Attached Listener 
    btnAdd.setOnClickListener(this); 
    btnUpdate.setOnClickListener(this); 
    btnDelete.setOnClickListener(this); 
    lvNotes.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapter, View v, 
       int position, long id) { 

      String title, desc, salary; 

      // Display Selected Row of Listview into EditText widget 

      Cursor row = (Cursor) adapter.getItemAtPosition(position); 
      selected_ID = row.getString(0); 
      title = row.getString(1); 
      desc = row.getString(2); 
      salary = row.getString(3); 

      txtTitle.setText(title); 
      txtDesc.setText(desc); 
      txtSalary.setText(salary); 
     } 
    }); 

    // Fetch Data from database 
    fetchData(); 
} 


@Override 
public void onClick(View v) { 

    // Perform CRUD Operation 

    if (v == btnAdd) { 

     // Add Record with help of ContentValues and DBHelper class object 
     ContentValues values = new ContentValues(); 
     values.put(DBHelper.C_TITLE, txtTitle.getText().toString()); 
     values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString()); 
     values.put(DBHelper.C_SALARY, txtSalary.getText().toString()); 

     // Call insert method of SQLiteDatabase Class and close after 
     // performing task 
     db = helper.getWritableDatabase(); 
     db.insert(DBHelper.TABLE, null, values); 
     db.close(); 

     clearFields(); 
     Toast.makeText(this.getActivity(), "Successfully Added", 
       Toast.LENGTH_LONG).show(); 

     // Fetch Data from database and display into listview 
     fetchData(); 

    } 
    if (v == btnUpdate) { 

     // Update Record with help of ContentValues and DBHelper class 
     // object 

     ContentValues values = new ContentValues(); 
     values.put(DBHelper.C_TITLE, txtTitle.getText().toString()); 
     values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString()); 
     values.put(DBHelper.C_SALARY, txtSalary.getText().toString()); 

     // Call update method of SQLiteDatabase Class and close after 
     // performing task 
     db = helper.getWritableDatabase(); 
     db.update(DBHelper.TABLE, values, DBHelper.C_ID + "=?", 
       new String[] { selected_ID }); 
     db.close(); 

     // Fetch Data from database and display into listview 
     fetchData(); 
     Toast.makeText(this.getActivity(), "Record Updated Successfully", 
       Toast.LENGTH_LONG).show(); 
     clearFields(); 

    } 
    if (v == btnDelete) { 

     // Call delete method of SQLiteDatabase Class to delete record and 
     // close after performing task 
     db = helper.getWritableDatabase(); 
     db.delete(DBHelper.TABLE, DBHelper.C_ID + "=?", 
       new String[] { selected_ID }); 
     db.close(); 

     // Fetch Data from database and display into listview 
     fetchData(); 
     Toast.makeText(this.getActivity(), "Record Deleted Successfully", 
       Toast.LENGTH_LONG).show(); 
     clearFields(); 

    } 
} 

// Clear Fields 
    private void clearFields() { 
     txtTitle.setText(""); 
     txtDesc.setText(""); 
     txtSalary.setText(""); 
    } 

// Fetch Fresh data from database and display into listview 
    private void fetchData() { 
     db = helper.getReadableDatabase(); 
     Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null); 
     adapter = new SimpleCursorAdapter(
       getActivity(), 
       R.layout.row, 
       c, 
       new String[] { DBHelper.C_TITLE, DBHelper.C_SALARY, 
         DBHelper.C_DESCRIPTION }, 
       new int[] { R.id.lblTitle, R.id.lblSalary, R.id.lblDescription }); 
     lvNotes.setAdapter(adapter); 
    } 
    } 

activity_favourite_fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Notes" 
    android:textAppearance="?android:attr/textAppearanceLarge" > 
</TextView> 

<EditText 
    android:id="@+id/txtTitle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Title" > 

    <requestFocus> 
    </requestFocus> 
</EditText> 

<EditText 
    android:id="@+id/txtDes" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Description" > 
</EditText> 

<EditText 
    android:id="@+id/txtSalary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Enter Salary" > 
</EditText> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/btnAdd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add" > 
    </Button> 

    <Button 
     android:id="@+id/btnUpdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Update Record" > 
    </Button> 

    <Button 
     android:id="@+id/btnDelete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Delete" > 
    </Button> 
</LinearLayout> 

<ListView 
    android:id="@+id/lvNotes" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</ListView> 

我剛開始了與androi d發展,任何幫助將不勝感激!

回答

3

你的問題是在onStart方法:

public void onStart() 
{ 
    super.onStart(); 
    initControls(); 
    // Init DB Objects 
    helper = new DBHelper(getActivity()); 
} 

請注意,您建立輔助對象,以後打電話initControls(),然後調用fetchData()

fetchData你有這樣一行:

db = helper.getWritableDatabase(); 

這是導致空指針異常,因爲助手是空行。

更改onStart方法,像這樣:

public void onStart() 
     { 
      super.onStart(); 
      // Init DB Objects 
      helper = new DBHelper(getActivity()); 
      initControls(); 

     } 

這應該解決您的問題。

+0

哦,我非常感謝你的解釋! – zana 2015-02-11 11:33:32

3

helper未初始化。

在您的onStart()中,您只需在致電initControls()後調用fetchData()即可使用helper進行初始化。在initControls()呼叫之前移動helper初始化。

+0

太感謝你了正確的地方! – zana 2015-02-11 11:34:41

0

比較好的解決辦法,招行代碼:

helper = new DBHelper(getActivity()); 

onStart()onCreate()哪裏是發起對象

+0

感謝您的幫助! – zana 2015-02-11 11:35:38

+0

onStart()將每次從不可見(背景)調用到可見(前景),但onCreate()只調用一次,所以你認爲應該啓動你的'helper'? – Xcihnegn 2015-02-11 14:25:20

相關問題