我試圖顯示一個首選項屏幕,如果我找不到保存的首選項。但由於空指針異常,我似乎遇到了我的應用程序崩潰問題。Try/Catch/Finally語句中的空指針異常
的代碼我目前正在使用
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
} catch (Exception e) {
// Preferences
Intent prefsIntent = new Intent(MainActivity.this,
Preferences.class);
startActivity(prefsIntent);
} finally {
Intent loginIntent = new Intent(MainActivity.this,
LoginForm.class);
startActivity(loginIntent);
}
}
}
編輯:
這是我從調試控制檯獲得。 http://pastebin.com/s0rEZEE9
從LoginForm.Java線24
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
編輯2:
這是整個LoginForm的是調試控制檯說是給了一個錯誤的東西。
package com.smashedbits.livestreams;
import java.util.HashMap;
import java.util.Map;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginForm extends Activity {
public AQuery aq;
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(LoginForm.this);
boolean autoLogin = sharedPrefs.getBoolean("remember_login", false);
EditText eUsername = (EditText) findViewById(R.id.usernameField);
EditText ePassword = (EditText) findViewById(R.id.passwordField);
@Override
public void onBackPressed() {
super.onBackPressed();
// return;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
aq = new AQuery(this);
// Views
final Button prefsButton = (Button) findViewById(R.id.prefsButton);
final Button loginButton = (Button) findViewById(R.id.loginButton);
// Check for saved data
String usrn = sharedPrefs.getString("usr", "NULL");
String pswd = sharedPrefs.getString("pwd", "NULL");
if (autoLogin == true & usrn != "NULL") {
eUsername.setText(usrn);
ePassword.setText(pswd);
}
// Preferences
prefsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent prefsIntent = new Intent(LoginForm.this,
Preferences.class);
startActivity(prefsIntent);
}
});
// Login
loginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ls_login();
}
});
}
public void ls_login() {
boolean autoLogin = sharedPrefs.getBoolean("remember_login", false);
if (autoLogin == true) { saveLogin(); }
String url = "http://{redacted}";
EditText eUsername = (EditText) findViewById(R.id.usernameField);
EditText ePassword = (EditText) findViewById(R.id.passwordField);
Map<String, Object> params = new HashMap<String, Object>();
params.put("log", eUsername.getText().toString());
params.put("pwd", ePassword.getText().toString());
params.put("rememberme", "forever");
params.put("wp-submit", "Log In");
params.put("redirect_to", "{redacted}");
params.put("testcookie", "1");
aq.ajax(url, params, String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String html, AjaxStatus status) {
if (html.contains("LOG OUT")) {
Intent guideIntent = new Intent(LoginForm.this,
ChannelGuide.class);
startActivity(guideIntent);
}
}
});
}
private void saveLogin() {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("usr", eUsername.getText().toString());
editor.putString("pwd", ePassword.getText().toString());
editor.commit();
}
}
請添加顯示'NullPointerException'的錯誤消息,並指出發生異常的代碼行。 – 2012-03-24 21:18:08
堆棧跟蹤是什麼樣的? – 2012-03-24 21:18:14
請始終提供您的logcat跟蹤錯誤.. – pleerock 2012-03-24 21:18:33