2013-07-11 13 views
0

嗨大家好, 我在使用NULL POINTER EXCEPTION時遇到了一些問題,僅在第一次將主題放置在主屏幕中時纔會出現錯誤。每隔一段時間小部件完美工作。 小部件具有配置活動。此活動不會啓動,而是在窗口小部件提供程序中的onReceive被調用,並通過NULL POINTER EXCEPTION。 任何人都可以提供一個很好的解決我的問題?在第一次啓動Widget時,Widget提供程序類中的NULL POINTER EXCEPTION

我附上配置活動和小部件提供程序類的代碼。

小部件配置類:

public class SingleNoteConfigure extends ListActivity { 


private NotesDbAdapter mDbHelper; 
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; 
public static String ACTION_WIDGET_LIST = "ActionReceiverList"; 
public static String ACTION_WIDGET_NEW = "ActionReceiverNew"; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //set the result to canceled to allow the user 
    //to change their mind mid widget configure 
    setResult(RESULT_CANCELED); 
    Log.d("MYTAG", "in the onCreate of of the widget configure"); 

    //set the layout file for the widget configure 
    setContentView(R.layout.notes_list_config); 

    //using the action bar title for user instruction 
    setTitle("Select note to display on the wodget"); 

    // Find the widget id from the intent. 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     mAppWidgetId = extras.getInt(
       AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 
    Log.d("MYTAG", "in the onCreate"); 
    //stuff to get the database to open 
    mDbHelper = new NotesDbAdapter(this); 
    mDbHelper.open(); 

    //call the method that fills the list view 
    fillData(); 

} 

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     Cursor note = mDbHelper.fetchNote(id); 
     startManagingCursor(note); 
     String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)); 
     String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)); 



     RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.singlenote_widget); 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
     appWidgetManager.updateAppWidget(mAppWidgetId, views);  



     Log.d("MYTAG", "in the onListItemClick...."); 

     loadData(title, text, id);//here id is the row id of the selection;it is returned by the onListItemSelect 

     Intent resultValue = new Intent(); 
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);    
     setResult(RESULT_OK,resultValue); 
     finish(); 
} 



void loadData(String title, String text, Long Id) { 

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
    Log.d("MYTAG", "in the load data...."); 
    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title, text); 
    NotesDbAdapter.updateWidgetId(mAppWidgetId,Id); 

} 

private void fillData() { 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 
    Log.d("MYTAG", "in the fill data of the widget configure"); 
    // Create an array to specify the fields we want to display in the list (TITLE and DATE) 
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_DATE}; 


    // and an array of the fields we want to bind those fields to (title and date) 
    int[] to = new int[]{R.id.title,R.id.date}; 


    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row1, notesCursor, from, to); 

    setListAdapter(notes); 


} 
} 

小部件提供類:

public class SingleNote extends AppWidgetProvider { 

public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget"; 

private static NotesDbAdapter mDbHelper; 



public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    final int N = appWidgetIds.length; 

    // Perform this loop procedure for each App Widget that belongs to this provider 
    for (int i=0; i<N; i++) { 
     int appWidgetId = appWidgetIds[i];    

     Log.d("MYTAG", "in the onUpdate"); 

     Intent intent = new Intent(context, Notepadv3.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget); 
     views.setOnClickPendingIntent(R.id.single_note_text, pendingIntent); 

     // Push update for this widget to the home screen 

     ComponentName thisWidget = new ComponentName(context, SingleNote.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(context); 
     manager.updateAppWidget(thisWidget, views); 

    } 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d("MYTAG", "in the onReceive...."); 
     String action = intent.getAction(); 
     Log.d("MYTAG", "in the onReceive....line 2"); 
     Bundle extras = intent.getExtras(); 
     Log.d("MYTAG", "in the onReceive....after bundle");//THis gets in the log 
     String title = extras.getString("title"); 
     Log.d("MYTAG", "in the onReceive....after title");//Non of the other log entry's 
                  // make it in 
     String text = extras.getString("body"); 
     Log.d("MYTAG", "in the onReceive....after text"); 
     Log.d("MYTAG", "@ the point of int"); 
     int id = extras.getInt("widget_id"); 

     Log.d("MYTAG", action+ title + text + id); 

     if (action != null && action.equals(UPDATE_ACTION)) { 
      final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

      if (id > 0) 
      { 
       updateWidget(context, appWidgetManager, id ,title ,text); 
      } 
      else { 
       return; 

      } 
    } 

    else { 
      super.onReceive(context, intent); 
    } 


} 

static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String title, String text){ 

    Log.d("MYTAG", "in the updatewidget method in the siglenote widget...."); 
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget); 
    views.setTextViewText(R.id.single_note_title, title); 
    views.setTextViewText(R.id.single_note_text, text); 
    appWidgetManager.updateAppWidget(appWidgetId, views); 



} 
} 

正如你可以看到我有日誌條目所有的地方,我在點已作了評論日誌條目結束。

此方法在其中一個活動中,我稱它爲根據需要更新我的小部件。 我有一個「如果(!空)」的方法剛剛結束,這樣是沒有得到空指針異常,但它似乎並不被幫助

private void updateWidget() { 
    Cursor note = mDbHelper.fetchWidgetId(mRowId); 
    startManagingCursor(note); 
    Log.d("MYTAG", "in the updatewidget method in edit note"); 
    int id = note.getInt(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_WIDGET_ID));//here id is the widget id we get out of the database 
    Intent i = new Intent(this, SingleNote.class); 
    i.setAction(SingleNote.UPDATE_ACTION); 
    i.putExtra("title", mTitleText.getText().toString()); 
    i.putExtra("body", mBodyText.getText().toString()); 
    i.putExtra("widget_id",id); 
    String title = mTitleText.getText().toString(); 
    //String body = mBodyText.getText().toString(); 
    if(title != null){ 
    sendBroadcast(i); 
    } 

編輯的堆棧跟蹤:

07-12 04:08:36.473: E/STACKTRACE_TAG(2615): STACKTRACE 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615): java.lang.NullPointerException 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at drkstr.yan.SingleNote.onReceive(SingleNote.java:58) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1994) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.access$2400(ActivityThread.java:132) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1093) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.os.Looper.loop(Looper.java:143) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.main(ActivityThread.java:4196) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at java.lang.reflect.Method.invokeNative(Native Method) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at java.lang.reflect.Method.invoke(Method.java:507) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    07-12 04:08:36.483: E/STACKTRACE_TAG(2615):  at dalvik.system.NativeStart.main(Native Method) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615): STACKTRACE 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615): java.lang.NullPointerException 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at drkstr.yan.SingleNote.onReceive(SingleNote.java:58) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1994) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.access$2400(ActivityThread.java:132) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1093) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.os.Looper.loop(Looper.java:143) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at android.app.ActivityThread.main(ActivityThread.java:4196) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at java.lang.reflect.Method.invokeNative(Native Method) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at java.lang.reflect.Method.invoke(Method.java:507) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    07-12 04:08:45.483: E/STACKTRACE_TAG(2615):  at dalvik.system.NativeStart.main(Native Method) 

這裏是我的主要問題:1。 爲什麼當我把小部件的第一次後重新安裝這個應用程序的唯一發生的呢? 2.爲什麼在widget配置之前調用onRecieve? 3.最後我該如何解決它。

感謝您花時間閱讀本文,並感謝您提供的任何幫助。

+1

當你正在尋找異常幫助,你應該提供你的堆棧跟蹤 – jqpubliq

+0

我完全同意@jqpubliq我們需要堆棧跟蹤和日誌將有所幫助... – Christos

+0

剛剛添加它,不知道是什麼堆棧跟蹤至今。有人可以解釋說,所有這些東西都意味着什麼?還有一個簡單的說明,在我添加了try後,這個小工具就可以正常工作了。 (APP_TAG,Log.getStackTraceString(e)); } – DrkStr

回答

1

你應該注意的主要事情是你有一個NullPointerExceptionSingleNote.java:58

從你的意見中可以看出哪些語句出現在日誌中,我猜extras爲空,這意味着有no extras in the Intent。如果它只發生在第一次,這是有道理的。在使用之前進行空值檢查。

相關問題