嘿伙计我不知道我在这里做错了什么但是当我在按钮上添加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();
}
}
}
}
答案 0 :(得分:1)
在检索您的UI元素之前移动setContentView
,因为您需要在findViewById
返回null
之前充气您的布局,因此行btnSpeichern.setOnClickListener
处的NPE
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);
答案 1 :(得分: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