2014-04-16 103 views
0

正在嘗試創建一個應用程序,從數據庫中提取數據並設置任務提醒,但在調用提醒函數時出現NullPointerException,我是Android新手請幫助我。登錄貓和相關的程序代碼已經給下面Null指針提醒器異常

logcat的

04-16 21:09:12.199: E/AndroidRuntime(1389): FATAL EXCEPTION: main 
04-16 21:09:12.199: E/AndroidRuntime(1389): java.lang.NullPointerException 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at com.examples.TaskRemainderActivity.remind(TaskRemainderActivity.java:49) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at com.examples.ReminderView$MyListAdapter.bindView(ReminderView.java:123) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.AbsListView.obtainView(AbsListView.java:2267) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.ListView.onMeasure(ListView.java:1156) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.View.measure(View.java:15172) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.View.measure(View.java:15172) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.View.measure(View.java:15172) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.View.measure(View.java:15172) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.View.measure(View.java:15172) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.Choreographer.doFrame(Choreographer.java:525) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.os.Handler.handleCallback(Handler.java:615) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.os.Looper.loop(Looper.java:137) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-16 21:09:12.199: E/AndroidRuntime(1389):  at dalvik.system.NativeStart.main(Native Method) 

ReminderView。 java的

package com.examples; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Date; 

import android.app.AlarmManager; 
import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.ResourceCursorAdapter; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Toast; 

public class ReminderView extends ListActivity implements OnClickListener { 
    Button add; 
    private String TitleName[]={"Edit","Delete"}; 
    private ArrayList<String> array_sort; 
    int textlength=0; 
    private AlertDialog myalertDialog=null; 
    Reminder_DB ex1 = new Reminder_DB(this); 
    public String event, venue, date, time, row_id, remind; 
    public String revent, rvenue, remind_on; 






    int flag =0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sqlist); 
     add = (Button) findViewById(R.id.baddContact); 
     add.setOnClickListener(this); 

     Reminder_DB dbHelper = new Reminder_DB(this); 
     dbHelper.open(); 
     // Get a Cursor for the list items 
      Cursor listCursor = dbHelper.fetchListItems(); 
      startManagingCursor(listCursor); 
      setListAdapter(new MyListAdapter(this, listCursor)); 
      //dbHelper.close(); 
      // TaskRemainderActivity.remind(revent, rvenue, remind_on); 
      // Log.e("Remind_On ==>",remind_on); 


    } 
    private class MyListAdapter extends ResourceCursorAdapter{ 
     public MyListAdapter(Context context, Cursor cursor) { 
      super(context, R.layout.list_item, cursor); 
    } 
     @Override 
      public void bindView(View view, Context context, final Cursor cursor) { 

       TextView tvevent = (TextView) view.findViewById(R.id.levent); 
       StringBuffer eventText = new StringBuffer(); 
       eventText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_EVENT))); 
       tvevent.setText(eventText.toString()); 

       TextView tvvenue = (TextView) view.findViewById(R.id.lvenue); 
       StringBuffer venueText = new StringBuffer(); 
       venueText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_VENUE))); 
       tvvenue.setText(venueText.toString()); 

       TextView tvdate = (TextView) view.findViewById(R.id.ldate); 
       StringBuffer dateText = new StringBuffer(); 
       dateText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_DATE))); 
       tvdate.setText(dateText.toString()); 

       TextView tvtime = (TextView) view.findViewById(R.id.ltime); 
       StringBuffer timeText = new StringBuffer(); 
       timeText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_TIME))); 
       tvtime.setText(timeText.toString()); 


       StringBuffer idText = new StringBuffer(); 
       idText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_ROWID))); 

       StringBuffer remindText = new StringBuffer(); 
       remindText.append(cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_REM))); 


       if(flag==0){ 
        revent=eventText.toString(); 
        rvenue=venueText.toString(); 
        remind_on=remindText.toString(); 
        Log.e("View_Date ==>",dateText.toString()); 
        Log.e("View_Time ==>",timeText.toString()); 
        Log.e("View_Venue ==>",rvenue); 
        Log.e("View_Event ==>",revent); 
        Log.e("View_ID ==>",idText.toString()); 
        Log.e("View_Remind_On ==>",remind_on); 
        flag=1; 

       } 




       //TaskRemainderActivity.remind(revent, rvenue, remind_on); 
       final ListView list = (ListView) findViewById(android.R.id.list); 

       TaskRemainderActivity.remind(revent, rvenue, remind_on); 

       // listening to single list item on click 
       list.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

         event = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_EVENT)); 
         venue = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_VENUE)); 
         date = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_DATE)); 
         time = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_TIME)); 
         row_id = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_ROWID)); 
         remind = cursor.getString(cursor.getColumnIndex(Reminder_DB.KEY_REM)); 

         AlertDialog.Builder myDialog = new AlertDialog.Builder(ReminderView.this); 

        final EditText editText = new EditText(ReminderView.this); 
        final ListView listview=new ListView(ReminderView.this); 

        array_sort=new ArrayList<String> (Arrays.asList(TitleName)); 
        LinearLayout layout = new LinearLayout(ReminderView.this); 
        layout.setOrientation(LinearLayout.VERTICAL); 

        layout.addView(listview); 
        myDialog.setView(layout); 
        CustomAlertAdapter arrayAdapter=new CustomAlertAdapter(ReminderView.this, array_sort); 
        listview.setAdapter(arrayAdapter); 

        myDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 
         } 
        }); 

        listview.setOnItemClickListener(new OnItemClickListener() { 
         public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
          myalertDialog.dismiss(); 
          if(position==0){ 
          Toast.makeText(ReminderView.this, 
            "Edit is pressed", Toast.LENGTH_SHORT) 
            .show(); 



           Intent i = new Intent(getApplicationContext(), ReminderSingleView.class); 

           // sending data to new activity 
          i.putExtra("event_name", event); 
          i.putExtra("event_venue", venue); 
          i.putExtra("event_date", date); 
          i.putExtra("event_time", time); 
          i.putExtra("rowid", row_id); 
          i.putExtra("remind", remind); 
           startActivity(i); 
          // int t = 1+ position; 
           Toast.makeText(ReminderView.this, 
             event, Toast.LENGTH_SHORT) 
             .show(); 

          } 
          if(position==1){ 
          Toast.makeText(ReminderView.this, 
            "Delete is pressed", Toast.LENGTH_SHORT) 
            .show(); 

          alertMessage(); 

          } 


         } 
        }); 

        myalertDialog=myDialog.show(); 



        } 
       }); 
      } 


    } 

    public void alertMessage() { 
     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       switch (which) { 

      case DialogInterface.BUTTON_POSITIVE: 
       // Yes button clicked 

        long lRow1 = Long.parseLong(row_id); 

        ex1.open(); 
        ex1.deleteEntry(lRow1); 
        ex1.close(); 
        Toast.makeText(ReminderView.this, 
          "DELETED!" , Toast.LENGTH_SHORT) 
          .show(); 
         finish(); 
         Intent intent = new Intent(ReminderView.this, ReminderView.class); 
         startActivity(intent); 

      break; 
      case DialogInterface.BUTTON_NEGATIVE: 
       // No button clicked 
       // do nothing 
       Toast.makeText(ReminderView.this, "No Clicked", Toast.LENGTH_LONG).show(); 
       break; 
       } 
      } 
     }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure?") 
    .setPositiveButton("Yes", dialogClickListener) 
    .setNegativeButton("No", dialogClickListener). 
    show(); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

      Intent intent = new Intent(ReminderView.this, Remainder.class); 
      startActivity(intent); 
    } 




    } 

TaskRemainderActivity.java

package com.examples; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class TaskRemainderActivity { 
    /** Called when the activity is first created. */ 

     private static Context mContext; 
     private static String Rtime,Revent,Rvenue; 
     public static int notificationCount; 

public static void remind(String event, String venue, String remind){ 


      Rtime=remind; 
      Revent=event; 
      Rvenue=venue; 

      Log.e("Rem_Event ==>",Revent); 
      Log.e("Rem_Venue ==>",Rvenue); 
      Log.e("Rem_timet ==>",Rtime); 

      notificationCount = notificationCount+1; 

      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh-mm"); 
      Date dt = null; 
      try { 
       dt = df.parse(Rtime); 

       Log.e("Rem_dt ==>",dt.toString()); 

       } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 


      long rem_when = dt.getTime();  
      Log.e("Rem_rem_when ==>",Long.toString(rem_when)); 

      AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
      Log.e("Rem_Alaram ==>",Integer.toString(1)); 
      Intent notificationIntent = new Intent(mContext, ReminderAlarm.class); 
      notificationIntent.putExtra("Name",Revent); 
      notificationIntent.putExtra("Description",Rvenue); 
      notificationIntent.putExtra("NotifyCount",notificationCount); 

      Log.e("Rem_Alaram ==>",Integer.toString(2)); 
      PendingIntent pi = PendingIntent.getBroadcast(mContext, notificationCount, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      mgr.set(AlarmManager.RTC_WAKEUP,rem_when, pi); 
      Toast.makeText(mContext, "Your Reminder Activated", Toast.LENGTH_LONG).show(); 

     } 


    } 
+0

檢查您的logcat並注意它指的是行放在taskremainderactivity.java 49;你的錯誤在於此。 – Tukajo

回答

1

試試這個。

您的mContext在您的TaskRemainderActivity.class中爲空發送具有如下構造函數的上下文。

TaskRemainderActivity.remind(revent, rvenue, remind_on, ReminderView.this); 

得到像下面

public static void remind(String event, String venue, String remind, Context context){ 

      mContext = context; 
+0

非常感謝你的工作,非常感謝 – Saran

+0

@ user3358634很高興幫助。快樂編碼。 – Hariharan