2013-03-01 13 views
0

幾乎所有的工作都在我一直在努力的程序中。當我在UI中進行更改時,所有設置都正確。由於我無法弄清楚的原因,當我點擊我的主要UI的輸入按鈕時,應用程序部隊關閉,但有一個例外。從日誌...警報對話框無法正確彈出。導致異常的按鈕。不確定爲什麼

java.lang.IllegalStateException:找不到在活動課com.loch.meaptracker.MainActivity爲的onClick處理程序的方法enterMood(視圖)和視圖類android.widget.Button與id'enterButtonID'

這是程序代碼。 Eclipse不會爲錯誤或警告標記任何內容。我有點不知所措,看着這個東西很枯燥。任何幫助將不勝感激。

package com.loch.meaptracker; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TimePicker; 

public class MainActivity extends Activity implements OnSeekBarChangeListener { 

private SeekBar happyBar, energyBar, anxietyBar, painBar; 
private EditText noteField; 
private DatePicker dPick; 
private TimePicker tPick; 
@SuppressWarnings("unused") 
private Button enterButton; 
private int happyValue = 4, energyValue = 4, anxietyValue = 4, 
     painValue = 4; 
private static final String TAG = "heapApp"; 
private String Mood = "Blah"; 
final Context context = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    try { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // bars 
     happyBar = (SeekBar) findViewById(R.id.happinessBarID); 
     happyBar.setOnSeekBarChangeListener(this); 
     energyBar = (SeekBar) findViewById(R.id.energyBarID); 
     energyBar.setOnSeekBarChangeListener(this); 
     anxietyBar = (SeekBar) findViewById(R.id.anxietyBarID); 
     anxietyBar.setOnSeekBarChangeListener(this); 
     painBar = (SeekBar) findViewById(R.id.painBarID); 
     painBar.setOnSeekBarChangeListener(this); 
     // end bars 
     dPick = (DatePicker) findViewById(R.id.datePicker1); 
     tPick = (TimePicker) findViewById(R.id.timePicker1); 
     noteField = (EditText) findViewById(R.id.noteTextFieldID); 
     enterButton = (Button) findViewById(R.id.enterButtonID); 
    } catch (Exception onCreateException) { 
     Log.e(TAG, "Exception received", onCreateException); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

// Bar listener methods 
@Override 
public void onProgressChanged(SeekBar arg0, int barValue, boolean hFromUser) { 
    try { 

     switch (arg0.getId()) { 
     case R.id.happinessBarID: 
      happyValue = barValue + 1; 
      break; 
     case R.id.energyBarID: 
      energyValue = barValue + 1; 
      break; 
     case R.id.anxietyBarID: 
      anxietyValue = barValue + 1; 
      break; 
     case R.id.painBarID: 
      painValue = barValue + 1; 
      break; 
     } 
     String debugBarValue = "Happy is " + happyValue + ", Energy is " 
       + energyValue + ", Anxiety is " + anxietyValue 
       + ", Pain is " + painValue + "."; 
     System.out.println(debugBarValue); 

    } catch (Exception BarValueException) { 
     Log.e(TAG, "Exception received", BarValueException); 
    } 

} 

@Override 
public void onStartTrackingTouch(SeekBar happyBar) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStopTrackingTouch(SeekBar happyBar) { 
    // TODO Auto-generated method stub 

} 

// end Bar listener methods 

// Enter Button listener Method 

public void dialogPop(View v) { 
    try { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       context); 

     // set Title 
     alertDialogBuilder.setTitle("title"); 

     // set dialog message 
     alertDialogBuilder.setMessage("You entered: " + getMood()) 
       .setCancelable(false).setPositiveButton("Okay", 
       // When Okay button clicked the write mood string to file 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int id) { 
           try { 
            // This is the string that should be 
            // written to file 
            String data = getMood(); 
            // This is the file that should be 
            // written to 
            File heapFile = new File("heapFile.csv"); 

            // if file doesn't exists, then create 
            // it 
            if (!heapFile.exists()) { 
             heapFile.createNewFile(); 
            } 

            // true = append file 
            FileWriter heapFileWritter = new FileWriter(
              heapFile.getName(), true); 
            BufferedWriter heapBufferWritter = new BufferedWriter(
              heapFileWritter); 
            heapBufferWritter.write(data); 
            heapBufferWritter.close(); 

            System.out.println("Done"); 

           } catch (IOException e) { 
            e.printStackTrace(); 
           } 
          } 

         }) 
       // If they press either the cancel button or the back button 
       // on their device (Same thing) then close the dialog and 
       // give the user a chance to change what they've entered 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, 
            int id) { 
           // TODO Auto-generated method stub 
           dialog.cancel(); 
          } 
         }); 

     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 

     // show it 
     alertDialog.show(); 
    } catch (Exception buttonListenerException) { 
     Log.e(TAG, "Exception received", buttonListenerException); 
    } 
    return; 
} 

public String getMood() { 
    try { 
     int month = dPick.getMonth(); 
     int day = dPick.getDayOfMonth(); 
     int year = dPick.getYear(); 
     int minute = tPick.getCurrentMinute(); 
     String moodAntePost = "AM"; 
     boolean hourType = tPick.is24HourView(); 
     int moodHour = tPick.getCurrentHour(); 
     if (hourType == false && moodHour > 12) { 
      moodHour = (moodHour - 12); 
      moodAntePost = "PM"; 
     } else if (hourType == false && moodHour <= 0) { 
      moodHour = 12; 
     } else { 
     } 
     String noteText = noteField.getText().toString(); 
     Mood = "Happiness," + happyValue + ",Energy," + energyValue 
       + ",Anxiety," + anxietyValue + ",Pain," + painValue 
       + ",Date," + month + "/" + day + "/" + year + ",Time," 
       + moodHour + ":" + minute + "," + moodAntePost + ",Note," 
       + noteText; 
     System.out.println(Mood); 
    } catch (Exception getMoodException) { 
     Log.e(TAG, "Exception received", getMoodException); 
    } 

    return Mood; 
} 

} 

回答

1

請檢查XML代碼。我覺得在你的XML吊牌有一個屬性已添加機器人:的onClick =「enterMood」 ....嘗試刪除它,並運行它

使用

安卓的onClick =「dialogPop」,而不是

+0

謝謝。這似乎已經成功了。雖然現在我收到了異常,因爲我的文件系統顯然是隻讀的,但我認爲這可能是因爲我忘記告訴模擬器模擬實際擁有SD卡。 – fauxfire76 2013-03-01 19:02:55

0

您的點擊處理程序格式不正確。它需要包含一個視圖參數,以使XML點擊處理程序正常工作。

public void getMood(View view){ 

} 
相關問題