所以我正在尝试创建一个共享首选项,该应用程序运行良好,然后突然间它崩溃了。
我已经把它归结为破坏代码的确切行,我不确定如何修复它。
有什么建议吗?
这是我目前的代码:
public class studentEmail extends Activity {
private WebView mWebview;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_email) ;
final SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
mWebview = new WebView(this);
final EditText emEdit = (EditText)findViewById(R.id.editEmail);
final EditText passEdit = (EditText)findViewById(R.id.editPass);
if(preferences.getString("username", null).isEmpty() && preferences.getString("password", null).isEmpty())
{
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getApplicationContext(), "Preference is empty :)", Toast.LENGTH_SHORT).show();
TextView text = (TextView)findViewById(R.id.textView1);
TextView textEmail = (TextView)findViewById(R.id.textViewEmail);
TextView textPass = (TextView)findViewById(R.id.textViewPass);
TextView textTitle = (TextView)findViewById(R.id.textViewTitle);
text.setTypeface(null, Typeface.BOLD_ITALIC);
textEmail.setTypeface(null, Typeface.BOLD_ITALIC);
textPass.setTypeface(null, Typeface.BOLD_ITALIC);
textTitle.setTypeface(null, Typeface.BOLD_ITALIC);
text.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26);
textEmail.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26);
textPass.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26);
textTitle.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, 40);
textTitle.setText("E-mail Login");
text.setText("\n\nPlease enter your Student E-mail and Password.\n");
textEmail.setText("E-Mail:");
textPass.setText("\nPassword:");
final EditText emailValidate = (EditText)findViewById(R.id.editEmail);
final TextView emailValMsg = (TextView)findViewById(R.id.emailValidate);
final EditText password = (EditText)findViewById(R.id.editPass);
emailValidate.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@mail+\\.+itsligo+\\.+ie+";
if (email.matches(emailPattern) && s.length() > 0)
{
Toast.makeText(getApplicationContext(), "Your e-mail is now valid! :)", Toast.LENGTH_SHORT).show();
// or
emailValMsg.setText("Valid email");
}
else
{
emailValMsg.setText("Invalid email !");
}
}
});
final Button button = (Button) findViewById(R.id.loginButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (emailValidate.getText().toString().trim().matches("") || password.getText().toString().trim().matches("")) {
if (emailValidate.getText().toString().trim().matches("") && password.getText().toString().trim().matches("")) {
Toast.makeText(getApplicationContext(), "Please enter an E-mail Address and Password", Toast.LENGTH_SHORT).show();
} else if (emailValidate.getText().toString().trim().matches("")) {
Toast.makeText(getApplicationContext(), "Please enter an E-mail Address", Toast.LENGTH_SHORT).show();
} else if (password.getText().toString().trim().matches("")) {
Toast.makeText(getApplicationContext(), "Please enter a Password", Toast.LENGTH_SHORT).show();
}
}
else {
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String webUrl = mWebview.getUrl();
CheckBox cbox1 = (CheckBox)findViewById(R.id.checkbox_store);
if (cbox1.isChecked())
{
}
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + emEdit.getText() + "';" + "})()");
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + passEdit.getText() + "';" + "})()");
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()");
}
});
mWebview.loadUrl("http://login.microsoftonline.com");
setContentView(mWebview);
Toast.makeText(getApplicationContext(), "This works!", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String webUrl = mWebview.getUrl();
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + preferences.getString("username", null).toString() + "';" + "})()");
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + preferences.getString("password", null).toString() + "';" + "})()");
mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()");
}
});
mWebview.loadUrl("http://login.microsoftonline.com");
setContentView(mWebview);
Toast.makeText(getApplicationContext(), "Preferences have something", Toast.LENGTH_SHORT).show();
}
}
}
错误:
02-14 19:20:06.130 632-632/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.PorjectStudent/com.example.PorjectStudent.studentEmail}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
02-14 19:20:06.130 632-632/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main
at com.example.PorjectStudent.studentEmail.onCreate(studentEmail.java:35)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
02-14 19:42:37.971 677-677/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.PorjectStudent/com.example.PorjectStudent.studentEmail}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.PorjectStudent.studentEmail.onCreate(studentEmail.java:39)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我真的很难过,解决方案可能很简单,但它整天都在破坏我的大脑所以我决定寻求帮助,因为我没有在几个小时内取得进展!
感谢您的帮助:)
答案 0 :(得分:2)
您必须在setContentView(R.layout.student_email);
之后立即致电super.onCreate(savedInstanceState);
。你将SharedPreferences
放在中间,你不能这样做。
作为一般规则,这两个句子必须是任何onCreate()
方法的第一个句子(至少在Activity
内)。
----编辑----
public class studentEmail extends Activity {
private WebView mWebview;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_email) ;
final SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //This line breaks the code
mWebview = new WebView(this);
final EditText emEdit = (EditText)findViewById(R.id.editEmail);
final EditText passEdit = (EditText)findViewById(R.id.editPass);
答案 1 :(得分:0)
您必须始终在setContentView(R.layout...) ;
之后启动自定义代码。