Hallo,
und zwar wir müssen von der schule aus eine Fahrtenbuch App
machen und seit neustem bekomme ich einen null pointer bei folgender
zeile:
findViewById(R.id.btnSavedDrives).setOnClickListener(new OnClickListener(){ //hier gibts n null pointer.
der ganze code:
Java
package fahrtenbuch.gui;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class FahrtenbuchActivity extends Activity {
private static enum BackKeyAction {QUIT, MAIN_MENU, NEW_DRIVES, SAVED_DRIVES}
private BackKeyAction backKeyAction;
private final String SAVE_FILE = "test.txt";
String inhalt = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setMainMenuView();
getSavedDrives();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if(backKeyAction == BackKeyAction.QUIT) {
// quit();
}
if(backKeyAction == BackKeyAction.MAIN_MENU) {
setMainMenuView();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
public void setMainMenuView(){
// this.setContentView(R.layout.maintest);
this.setContentView(R.layout.main);
this.backKeyAction = BackKeyAction.QUIT;
findViewById(R.id.btnNewDrive).setOnClickListener(new OnClickListener(){
public void onClick(View v){
setNewDriveView();
}
});
findViewById(R.id.btnSavedDrives).setOnClickListener(new OnClickListener(){ //hier gibts n null pointer.
public void onClick(View v){
getSavedDrives();
}
});
}
public String gt() {
Timestamp timestamp = new Timestamp(new Date(0).getTime());
System.out.println(timestamp.toString());
return timestamp.toString();
}
public void getSavedDrives(){
this.setContentView(R.layout.gespeichertefahrten);
this.backKeyAction = BackKeyAction.MAIN_MENU;
String content = "";
System.out.println("load");
FileInputStream file;
ArrayList<String> data = new ArrayList<String>();
ArrayList<String[]> details = new ArrayList<String[]>();
try {
file = openFileInput(SAVE_FILE);
try {
String line;
Integer a = 0;
Integer b = 1;
DataInputStream dis = new DataInputStream(file);
while((line=dis.readLine())!=null){
Log.v("Outp", line);
String[] splitArray = line.split(";");
// data.add(splitArray[0] + " " + splitArray[3] + " - " + splitArray[4]);
data.add(splitArray[0]);
details.add(splitArray);
}
file.close();
} catch(IOException e) {e.printStackTrace();}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(content);
String[] array = new String[data.size()];
for(int i = 0; i < data.size(); i++) {
array[i] = data.get(i);
}
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<String>(this, R.id.listItemTV,array));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
public void setNewDriveView(){
this.setContentView(R.layout.newdrive);
this.backKeyAction = BackKeyAction.MAIN_MENU;
gt();
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
public void onClick(View view){
setNewDriveView();
}
});
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View view){
try {
TextDateiSpeicherung.createFile();
inhalt = TextDateiSpeicherung.loadFromFile(openFileInput(SAVE_FILE));
TextDateiSpeicherung.saveToFile(openFileOutput(SAVE_FILE, Context.MODE_PRIVATE), inhalt + "10.06.2012" + ";" +
"12:25" + ";" +
"12:35" + ";" +
((EditText) findViewById(R.id.editText1)).getText().toString() + ";" +
((EditText) findViewById(R.id.editText2)).getText().toString() + ";" +
((EditText) findViewById(R.id.editText3)).getText().toString() + "\n");
Toast.makeText(getBaseContext(),"Fahrt gespeichert",Toast.LENGTH_SHORT).show();
setNewDriveView();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
Alles anzeigen
Wäre euch echt dankbar wenn mir jemand helfen könnte.