2014-01-28 43 views
1

嘿傢伙我不知道我在這裏做錯了什麼,但是當我添加一個onClickListener到一個按鈕時,我的活動崩潰了。如果我刪除監聽或嘗試在他的另一項活動一切正常......當我添加OnClickListener時得到一個異常

01-28 18:29:56.906: E/AndroidRuntime(1693): FATAL EXCEPTION: main 
01-28 18:29:56.906: E/AndroidRuntime(1693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doublexxsoftware.dreamtimer/com.doublexxsoftware.dreamtimer.Addnewdream}: java.lang.NullPointerException 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.os.Looper.loop(Looper.java:137) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at dalvik.system.NativeStart.main(Native Method) 
01-28 18:29:56.906: E/AndroidRuntime(1693): Caused by: java.lang.NullPointerException 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at com.doublexxsoftware.dreamtimer.Addnewdream.onCreate(Addnewdream.java:44) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.Activity.performCreate(Activity.java:5008) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
01-28 18:29:56.906: E/AndroidRuntime(1693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 

以及活動類:

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import android.os.Bundle; 
import android.os.Environment; 
import android.widget.Button; 
import android.widget.EditText; 
import android.app.Activity; 

public class Addnewdream extends Activity { 
    private Button btnSpeichern; 
    private EditText startDatum; 
    private EditText endDatum; 
    private EditText beschreibung; 
    private EditText dreamName; 
    private File dir; 

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

     startDatum = (EditText) findViewById(R.id.startDatum); 
     endDatum = (EditText) findViewById(R.id.endDatum); 
     beschreibung = (EditText) findViewById(R.id.beschreibung); 
     dreamName = (EditText) findViewById(R.id.dreamname); 
     btnSpeichern = (Button) findViewById(R.id.btnSpeichern); 

     setContentView(R.layout.activity_addnewdream); 

     String state = Environment.getExternalStorageState(); 
     if (!state.equals(Environment.MEDIA_MOUNTED)) { //Falls keine externe SD Karte vorhanden ist 
      dir = new File(getFilesDir().getAbsolutePath() + File.separator + "DreamTimer"); //Neuen Ordner für Daten auf internen Speicher erstellen 
      dir.mkdir(); 
     } else { 
      dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "DreamTimer"); //Sonst auf der externen SD Karte 
      dir.mkdir(); 
     } 

btnSpeichern.setOnClickListener(new View.OnClickListener() { 

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

      } 
     }); 




    } 

    private void dreamspeichern(String startDat, String endDat, String besch, String name) { 
     File file = new File(dir.getAbsolutePath(), name + ".txt"); 
     BufferedWriter writer = null; 
     try { 
      writer = new BufferedWriter(new FileWriter(file)); 
      writer.write(name); 
      writer.newLine(); 
      writer.write(startDat); 
      writer.newLine(); 
      writer.write(endDat); 
      writer.newLine(); 
      writer.write(besch); 
      writer.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      try { 
       if(writer != null) 
        writer.close(); 
        writer = null; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 
} 

回答

1

移動setContentView檢索您的UI元素,因爲你需要充氣佈局之前之前在該行否則findViewById返回null,因此NPE btnSpeichern.setOnClickListener

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_addnewdream); 

    startDatum = (EditText) findViewById(R.id.startDatum); 
    endDatum = (EditText) findViewById(R.id.endDatum); 
    beschreibung = (EditText) findViewById(R.id.beschreibung); 
    dreamName = (EditText) findViewById(R.id.dreamname); 
    btnSpeichern = (Button) findViewById(R.id.btnSpeichern); 
+0

該死的我很笨>。 Ihara

1

陳通用電氣

setContentView(R.layout.activity_addnewdream) 
    startDatum = (EditText) findViewById(R.id.startDatum); 
    endDatum = (EditText) findViewById(R.id.endDatum); 
    beschreibung = (EditText) findViewById(R.id.beschreibung); 
    dreamName = (EditText) findViewById(R.id.dreamname); 
    btnSpeichern = (Button) findViewById(R.id.btnSpeichern); 

您需要設置佈局到活動的內容,然後初始化意見findViewById查找當前膨脹的佈局的視圖。如果不是你最終得到NullPointerExcpetion

相關問題