2011-12-27 31 views
-1

12-27 16:57:11.711:E/AndroidRuntime(22081):引起:java.lang.ClassCastException:com.mygps.android.AlarmReceiver。java.lang.ClassCastException

這是我在logcat中的一個錯誤。什麼是錯誤,我如何解決它。

示例代碼:

public class MainActivity extends Activity { 
     private int currentIntervalChoice = 0; 

    @Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     setAppInfo(); 
     addButtonListeners(); 
     enableControls(); 
    } 

     private void addButtonListeners() { 
       ((Button)findViewById(R.id.start_logging)).setOnClickListener(btnClick); 
        ((Button)findViewById(R.id.logging_interval)).setOnClickListener(btnClick); 
     } 

     private void setAppInfo() { 
       TextView txtInfo = (TextView)findViewById(R.id.app_info); 

     txtInfo.setText(Html.fromHtml(getString(R.string.app_info))); 

     Linkify.addLinks(txtInfo, Linkify.ALL); 
     } 

     private void toggleLogging(boolean isStart, int interval){ 
       AlarmManager manager = (AlarmManager)getSystemService(Service.ALARM_SERVICE); 
      PendingIntent loggerIntent = PendingIntent.getBroadcast(this, 0,new Intent(this,AlarmReceiver.class), 0); 

      if(isStart){ 
        manager.cancel(loggerIntent); 

        AppSettings.setServiceRunning(this, false); 

        AppLog.logString("Service Stopped."); 
      } 
      else{ 
        setLogFileName(); 

        long duration = interval * 60 * 1000; 

        manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
            SystemClock.elapsedRealtime(), duration, loggerIntent); 

        AppSettings.setServiceRunning(this, true); 

        AppLog.logString("Service Started with interval " + interval + ", Logfile name: " + AppSettings.getLogFileName(this)); 
      } 
    } 

    private void enableControls(){ 
      boolean isServiceRunning = AppSettings.getServiceRunning(this); 
      String buttonText = getString(R.string.start_logging); 

      if(isServiceRunning){ 
        buttonText = getString(R.string.stop_logging); 

        ((Button)findViewById(R.id.logging_interval)).setEnabled(false); 
      } 
      else{ 
        ((Button)findViewById(R.id.logging_interval)).setEnabled(true); 
      } 

      ((Button)findViewById(R.id.start_logging)).setText(buttonText); 
    } 

    private void changeLoggingIntercal(){ 
      final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      final String loggingIntervals[] = { "5 minutes", "15 minutes", "30 minutes", "1 hour" }; 

    builder.setTitle(getString(R.string.logging_interval)); 
    builder.setSingleChoiceItems(loggingIntervals, currentIntervalChoice, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
          currentIntervalChoice = which; 

          setLoggingInterval(currentIntervalChoice); 

          dialog.dismiss(); 
        } 
      }); 

    builder.show(); 
    } 

    private void setLoggingInterval(int intervalChoice){ 
      int interval = 5; 

      switch(intervalChoice){ 
        case 0:   interval = 5; break; 
        case 1:   interval = 15; break; 
        case 2:   interval = 30; break; 
        case 3:   interval = 60; break; 
        default:  interval = 5; break; 
      } 

      AppSettings.setLoggingInterval(this, interval); 
    } 

    public void setLogFileName(){ 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      String dateString = sdf.format(new Date()); 
      String filename = "GPSLog." + dateString + ".kml"; 

      AppSettings.setLogFileName(this, filename); 
    } 

    private View.OnClickListener btnClick = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        switch(v.getId()) 
        { 
          case R.id.start_logging:{ 
            toggleLogging(AppSettings.getServiceRunning(MainActivity.this), 
                   AppSettings.getLoggingInterval(MainActivity.this)); 

            enableControls();  

            break; 
          } 
          case R.id.logging_interval:{ 
            changeLoggingIntercal(); 

            break; 
          } 
        } 
      } 
    }; 

}

main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dip"> 


    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:id="@+id/app_info" 
    android:layout_weight="1.0"/> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:padding="10dip"> 

    <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/start_logging" 
      android:text="@string/start_logging" 
      android:layout_weight="1.0"/> 

    <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/logging_interval" 
      android:text="@string/logging_interval" 
      android:layout_weight="1.0"/> 

</LinearLayout> 

logfile.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dip"> 


    <EditText 
     android:id="@+id/edit" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Enter log filename here." /> 

+0

我不知道你是否發佈了正確的代碼。導致錯誤的線是什麼? – 2011-12-27 11:33:39

+0

在你的代碼中沒有投射,我建議你看看錯誤所在的那一行,並給我們給定的代碼(可能在子函數enableControls()或changeLoggingIntercal()中。 – AsTeR 2011-12-27 11:34:35

+0

我編輯了我的代碼,這是我正在使用的完整代碼,請查看編輯過的代碼,告訴我我哪裏出錯了,同時告訴我如何保存從服務中獲得的GPS記錄器值 – 2011-12-27 11:43:44

回答

0

請發佈與此關聯的xml佈局文件和完整的logcat。我的建議,雖然這可能並不重要,但是要停止使用((Button)findViewById(R.id.logging_interval))引用您的控件的方法,並只使用一個變量。我只能說這是因爲我看到了錯誤,當機器人將此解釋爲企圖重塑和怪胎

0

附加報警接收機類清單文件

<receiver android:name=".AlarmReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> 
</receiver> 

我想與您的按鈕的

問題獲取按鈕的onCreate(),然後使用那些按鈕的地方你需要..

+0

我已經添加了我的xml文件,請查看佈局並告訴我我錯在哪裏。 – 2011-12-27 12:01:58

+0

更改此行Button a =(Button)findViewById(R.id.start_logging);在onCreate()中並使用該名稱在聽衆中。 – 2011-12-27 12:04:44

+0

沒有用。仍然是同樣的錯誤。 – 2011-12-27 12:15:38

相關問題