Android - 引起:java.lang.NullPointerException:storage == null

时间:2015-08-07 08:18:38

标签: android

“我正在尝试从数据库中检索数据并添加到我的listAdapter中。一切正常,而listAdapter不为空。但是,当listAdapter为空时,我得到运行时异常。我已经尝试了几个小时但是可以'得到一个解决方案。我尝试过的解决方案,如setEmptyView,setEmptyText,throw Exception等。抱歉,我是编程新手。“

这是我的ListFragment(LecMonFragmentTab.java):

public class LecMonFragmentTab extends ListFragment{
DBAdapter dbAdapter;

 public LecMonFragmentTab() {

 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  dbAdapter = new DBAdapter(getActivity());
  dbAdapter.open();
  String myData[] = dbAdapter.getScheduleRecord("1");  
  ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myData);
  setListAdapter(listAdapter);

  } 

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  return inflater.inflate(R.layout.lec_mon_fragment_tab, container, false);
 }

 @Override
 public void onListItemClick(ListView list, View v, int position, long id) {

  Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
 }
}

lec_mon_fragment_tab.xml

<LinearLayout 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:orientation="vertical" 
android:paddingLeft="8dp" 
android:paddingRight="8dp" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.fyp2.LecMonFragmentTab" >

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

 </ListView>

将对DBAdapter

public String[] getScheduleRecord(String lecid) {
    // TODO Auto-generated method stub

    // Get calendar set to current date and time
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, -15);

    // Print dates of the current week starting on Sunday
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    //sdf.format(c.getTime());
    SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss a");

     try {
        String arrData[] = null;

        String strSQL = "SELECT * FROM " + TABLE_SCHEDULE + " WHERE lecturer_id=? AND schedule_day=? AND schedule_endtime > ?"; 
         Cursor cursor = mDb.rawQuery(strSQL, new String[]{lecid, sdf.format(c.getTime()), sdfTime.format(c.getTime())});

            if(cursor != null)
            {
                if (cursor.moveToFirst()) {
                    arrData = new String[cursor.getCount()];

                    int i= 0;
                    do {                
                        arrData[i] = cursor.getString(1) 
                                 + " - " + cursor.getString(2);
                        i++;
                    } while (cursor.moveToNext());                      

                }
            }

            cursor.close();

            return arrData;

     } catch (Exception e) {
        return null;
     }
}

LogCat输出

08-07 04:48:19.271: E/AndroidRuntime(25461): FATAL EXCEPTION: main
08-07 04:48:19.271: E/AndroidRuntime(25461): Process: com.example.fyp2, PID: 25461
08-07 04:48:19.271: E/AndroidRuntime(25461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fyp2/com.example.fyp2.LecTakeAtt}: java.lang.NullPointerException: storage == null
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.os.Looper.loop(Looper.java:136)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.main(ActivityThread.java:5017)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.lang.reflect.Method.invoke(Method.java:515)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at dalvik.system.NativeStart.main(Native Method)
08-07 04:48:19.271: E/AndroidRuntime(25461): Caused by: java.lang.NullPointerException: storage == null
08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.util.Arrays.asList(Arrays.java:155)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.example.fyp2.LecMonFragmentTab.onCreate(LecMonFragmentTab.java:43)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.Activity.performStart(Activity.java:5241)
08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
08-07 04:48:19.271: E/AndroidRuntime(25461):    ... 11 more

希望大家都能给出一些提示或提供一些解决方案。欣赏它。

2 个答案:

答案 0 :(得分:1)

在您的主要活动中更改此内容:

if(myData.length != 0){
  ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myData);
  setListAdapter(listAdapter);
}

修改

也在DBAdapter中进行此更改:

if(cursor != null){
  cursor.close();
}

答案 1 :(得分:1)

检查myData的空状态,然后更新适配器。

if(myData!=null && mydata.length>0){
  setListAdapter(listAdapter);
}else{
  // Some messages
}