我想创建一个对话框,提示用户键入他/她的名字。 它将首次捕获使用它的用户名,并在用户登录时显示用户名。
我完成了编码, 但是发生了错误。 我什么时候开始申请, 有一个力量关闭错误。
logcat的:
01-25 01:03:24.023: E/AndroidRuntime(8242): FATAL EXCEPTION: main
01-25 01:03:24.023: E/AndroidRuntime(8242): android.app.SuperNotCalledException: Activity {com.example.fuellogproject/com.example.fuellogproject.homeActivity} did not call through to super.onCreate()
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2076)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.app.ActivityThread.access$700(ActivityThread.java:140)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.os.Looper.loop(Looper.java:137)
01-25 01:03:24.023: E/AndroidRuntime(8242): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-25 01:03:24.023: E/AndroidRuntime(8242): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 01:03:24.023: E/AndroidRuntime(8242): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 01:03:24.023: E/AndroidRuntime(8242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-25 01:03:24.023: E/AndroidRuntime(8242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-25 01:03:24.023: E/AndroidRuntime(8242): at dalvik.system.NativeStart.main(Native Method)
homeactivity.java
public class homeActivity extends Activity{
Button btnLogIn;
Button btnAbout;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState)
{
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
String userName = prefs.getString("user_name", null);
if (userName == null) {
EditText input = new EditText(this);
input.setId(1000);
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(input).setTitle("Enter your username!")
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
EditText theInput = (EditText) ((AlertDialog) dialog)
.findViewById(1000);
String enteredText = theInput.getText()
.toString();
if (!enteredText.equals("")) {
SharedPreferences.Editor editor = prefs
.edit();
editor.putString("user_name",
enteredText);
editor.commit();
}
}
}).create();
dialog.show();
}
}
}
答案 0 :(得分:0)
你在onCreate()方法中错过了 super.onCreate() .. ....