2012-03-24 144 views
0

我试图显示一个首选项屏幕,如果我找不到保存的首选项。但由于空指针异常,我似乎遇到了我的应用程序崩溃问题。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(); 
} 
} 
+1

请添加显示'NullPointerException'的错误消息,并指出发生异常的代码行。 – 2012-03-24 21:18:08

+0

堆栈跟踪是什么样的? – 2012-03-24 21:18:14

+0

请始终提供您的logcat跟踪错误.. – pleerock 2012-03-24 21:18:33

回答

0

我发现我只能在活动启动后声明变量,然后在oncreate下赋值它们。

1

没有堆栈跟踪,我的猜测是,无论是你的catch还是最后的原因。任何时候,如果你把逻辑放在一个catch/finally里面,你就有可能再次抛出错误,从而否定捕获到的异常。你总是可以试着把你的catch/finally块封装在一个什么也不做的try/catch中(或者肯定会导致另一个错误的东西)

你的错误很可能来自于新兴的Intent,访问其中一个参数用于新建它(MainActivity.this,LoginForm.class)或从startActivity(loginIntent)。那些是我至少会看到的地方

+0

我在哪里可以找到堆栈跟踪?这是我第一次尝试进入Java。 – mike 2012-03-24 22:47:01

+0

@mike我不确定。这不是我的语言,但它是一个通用编程问题。对不起,有没有可以使用的调试器? – 2012-03-24 23:02:40

+0

只需从Eclipse Logcat窗口发布logcat输出即可 – 2012-03-25 19:48:19