Ne sry, es hat jetzt geklappt Christian, aber wenn ich mit einer anderen Activity auf die Variable zugreifen will geht es nicht.
public class Probe extends Activity {
public static SharedPreferences name;
public static String Data;
public static String dataa;
public String getStringValue() {
return dataa;
}
public void setStringValue(String Value) {
dataa = Value;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.probe);
Button speichern = (Button) findViewById(R.id.save);
final TextView TextViewA= (TextView)findViewById(R.id.tVa);
final EditText name= (EditText) findViewById(R.id.editText1);
final SharedPreferences settings;
final SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
editor.putString("sharedString", Data);
editor.commit();
speichern.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
Data = firstplayer.getText().toString();
final String dataa = settings.getString("sharedString", Data);
TextViewA.setText("Name:"+ dataa);
Intent start = new Intent(getApplicationContext(), hauptmenu.class);
startActivity(start);
}
});
}
}
public class hauptmenu extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hauptmenu);
Probe appState = ((Probe)getApplicationContext());
String state = appState.getStringValue();
TextView Name = (TextView)findViewById(R.id.textviewName);
Name.setText(state);
}
}
Wenn ich jetzt auf den Button Speichern drücke wird die App geschlossen
10-21 13:05:19.025: E/AndroidRuntime(14790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yy.xxx/com.yy.xxx.hauptmenu}: java.lang.ClassCastException: android.app.Application cannot be cast to com.yy.xxx.Probe
MfG
— geändert am 21.10.2012, 13:15:48