2014-01-18 42 views
0

一開始一切工作完全正常..但後1個月不打開應用程序,這個錯誤出現。我沒有做任何改變。這是錯誤,我嘗試修復它。但仍然,問題是有..這個錯誤持續出現

01-18 17:54:43.271: W/dalvikvm(1244): threadid=1: thread exiting with uncaught exception (group=0xb4a51b90) 
01-18 17:54:44.401: D/dalvikvm(1244): GC_FOR_ALLOC freed 124K, 7% free 3060K/3272K, paused 764ms, total 782ms 
01-18 17:54:44.401: E/AndroidRuntime(1244): FATAL EXCEPTION: main 
01-18 17:54:44.401: E/AndroidRuntime(1244): Process: com.sabry.javabmi, PID: 1244 
01-18 17:54:44.401: E/AndroidRuntime(1244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sabry.javabmi/com.sabry.javabmi.bmiLogger}: java.lang.NullPointerException 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.os.Looper.loop(Looper.java:137) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at dalvik.system.NativeStart.main(Native Method) 
01-18 17:54:44.401: E/AndroidRuntime(1244): Caused by: java.lang.NullPointerException 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at com.sabry.javabmi.bmiLogger.initializeApp(bmiLogger.java:60) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at com.sabry.javabmi.bmiLogger.onCreate(bmiLogger.java:37) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.Activity.performCreate(Activity.java:5243) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
01-18 17:54:44.401: E/AndroidRuntime(1244):  ... 11 more 
01-18 17:54:52.581: I/Process(1244): Sending signal. PID: 1244 SIG: 9 

import java.io.File; 
import java.io.FileOutputStream; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Locale; 

import com.sabry.javabmi.R; 
import com.sabry.javabmi.bmiLogger; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.TimePickerDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TimePicker; 
import android.widget.Toast; 

public class bmiLogger extends Activity { 

private Intent currentIntent; 
private EditText edtWeight; 
private EditText edtTime; 
private EditText edtDate; 
private Button btnChart; 
private Calendar c; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bmi); 
    initializeApp(); 
} 

private void initializeApp() { 
    // TODO Auto-generated method stub 
    edtWeight = (EditText) findViewById(R.id.edtWeight); 
    edtDate = (EditText) findViewById(R.id.edtDate); 
    edtTime = (EditText) findViewById(R.id.edtTime); 

    c = Calendar.getInstance(); 

    currentIntent = getIntent(); 
    Bundle extras = currentIntent.getExtras(); 

    if(extras != null){ 
    edtWeight.setText(extras.getString("weight")); 
    } 

    setCurrentDateOnView(); 
    String FILENAME = "bmi_log.csv"; 
    File file = getApplicationContext().getFileStreamPath(FILENAME); 
    if(!file.exists()) { 
     // Disable the Chart Button 
     btnChart.setEnabled(false); 
    } 

} 


DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
     c.set(Calendar.YEAR, year); 
     c.set(Calendar.MONTH, monthOfYear); 
     c.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     setCurrentDateOnView(); 
    } 
    }; 

    TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      c.set(Calendar.HOUR_OF_DAY, hourOfDay); 
      c.set(Calendar.MINUTE, minute); 
      setCurrentDateOnView(); 
     } 
     }; 

public void dateOnClick(View view) { 
     new DatePickerDialog(bmiLogger.this, date, 
      c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH )).show(); 
    } 

public void timeOnClick(View view) { 
    new TimePickerDialog(bmiLogger.this, time, 
     c.get(Calendar.HOUR), c.get(Calendar.MINUTE), false).show(); 
    } 

public void setCurrentDateOnView(){ 
    //First alternative to make date and time 
    //int month, day, year, hours, minutes ; 
    //year = c.get(Calendar.YEAR); 
    //month = c.get(Calendar.MONTH); 
    //day = c.get(Calendar.DAY_OF_MONTH); 
    //hours = c.get(Calendar.HOUR); 
    //minutes = c.get(Calendar.MINUTE); 

    //Second approach 
    String dateFormat = "dd-MM-yyyy"; 
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US); 
    edtDate.setText(sdf.format(c.getTime())); 

    String timeFormat = "hh:mm a"; 
    SimpleDateFormat stf = new SimpleDateFormat(timeFormat, Locale.US); 
    edtTime.setText(stf.format(c.getTime())); 

} 

public void saveLogOnClick(View view) { 
    String FILENAME = "bmi_log.csv"; 
    String entry = edtDate.getText().toString() + "," + 
     edtTime.getText().toString() + "," + 
     edtWeight.getText().toString() + "\n"; 
    try { 
     FileOutputStream out = openFileOutput(FILENAME, Context.MODE_APPEND); 
     out.write(entry.getBytes()); 
     out.close(); 
     toastIt("Entry Saved"); 
     btnChart.setEnabled(true); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 


private void toastIt(String msg) { 
    // TODO Auto-generated method stub 
    Toast.makeText(bmiLogger.this, msg, Toast.LENGTH_LONG).show(); 

} 

public void switchToCalc(View v){ 
    finish(); 
} 

} 
+3

at'bmiLogger.java'的第60行是什麼? –

+0

該文件的第60行沒有任何內容,但假設它是正確的文件並且行號稍微偏離,我建議在您試圖打開bmi_log.csv並查看是否成功的位置進行突破。 –

回答

0

的錯誤是在這裏:

btnChart.setEnabled(false); 

你沒有初始化/設置你的btnChart OBJE克拉。

0

在下面的logcat中表示問題出現在第60行,這表明btnChart的初始化是一個問題。在你的代碼中,沒有出現btnChart初始化的地方。

bmiLogger.JavainitializeApp()初始化btnChart和檢查

at dalvik.system.NativeStart.main(Native Method) 
01-18 17:54:44.401: E/AndroidRuntime(1244): Caused by: java.lang.NullPointerException 
01-18 17:54:44.401: E/AndroidRuntime(1244):  at com.sabry.javabmi.bmiLogger.initializeApp(bmiLogger.java:60) 
0

我想你忘記了使用它之前初始化按鈕對象。

btnChart = (Button) findViewById(R.id.buttonId); 
相關問題