Try / Catch / Finally语句中的空指针异常

时间:2012-03-24 21:16:15

标签: java android

如果找不到我保存的偏好设置,我正在尝试显示首选项屏幕。但是由于Null指针异常,我似乎遇到了我的应用程序崩溃的问题。

我目前正在尝试使用的代码

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();
}
}

2 个答案:

答案 0 :(得分:1)

没有堆栈跟踪,我的猜测是你的捕获或最终是原因。无论何时将逻辑置于catch / finally中,都会冒再次抛出错误的风险,从而否定捕获的异常。你总是可以尝试将catch / finally块包装在一个什么都不做的try / catch中(或者肯定会导致另一个错误的东西)

您的错误最有可能来自新建Intent,访问用于启动它的其中一个参数(MainActivity.this,LoginForm.class)或来自startActivity(loginIntent)。那些是我至少看的地方

答案 1 :(得分:0)

我发现只能在活动开始后声明变量,然后在oncreate下分配它们。