1

我目前遇到来自 Logcat 的以下错误。我已经查看了十多个与此问题类似的其他帖子,但找不到解决我的问题的方法。

03-22 17:55:58.777: E/Trace(1172): error opening trace file: No such file or directory (2)
03-22 17:55:58.947: E/AndroidRuntime(1172): FATAL EXCEPTION: main
03-22 17:55:58.947: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.schoolwork.capstoneproject/com.schoolwork.capstoneproject.StartingPoint}: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:137)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:511)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.schoolwork.capstoneproject.StartingPoint.<init>(StartingPoint.java:11)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.Class.newInstanceImpl(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.Class.newInstance(Class.java:1319)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-22 17:55:58.947: E/AndroidRuntime(1172):     ... 11 more

这是我的java代码:

package com.schoolwork.capstoneproject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class StartingPoint extends Activity{

    SharedPreferences sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.starting_point);

        Thread verifyStatus = new Thread(){
            public void run(){
                try{
                    sleep(2000);
                }catch(Exception e){
                    e.printStackTrace();
                    Log.e("Thread Error", e.toString());
                }finally{
                    if(sharedPreferences.getBoolean("isLoggedIn", false)){
                        Intent openMainMenu = new Intent(getApplicationContext(), MainMenu.class);
                        openMainMenu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(openMainMenu);
                    }else{
                        Intent openLoginScreen = new Intent(getApplicationContext(), LoginScreen.class);
                        openLoginScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(openLoginScreen);
                    }
                } //end of try sequence
            }
        }; //end of thread
        verifyStatus.start();
    } //end of onCreate

}

我完全被卡住了,我找不到问题,这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.schoolwork.capstoneproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/prog_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LoginScreen"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name=".MainMenu"
            android:label="@string/app_name" >
        </activity>

    </application> 

</manifest>

谢谢你的帮助。

4

3 回答 3

6

logcat 很清楚。

03-22 17:55:58.947: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.schoolwork.capstoneproject.StartingPoint.<init>(StartingPoint.java:11)

您正在尝试在方法之外执行代码。这将不起作用:

SharedPreferences sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

反而:

SharedPreferences sharedPreferences;

并在 onCreate()

sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);
于 2013-03-22T22:13:16.657 回答
1

你有没有试过getSharedPreferences(...)投入onCreate()

public class StartingPoint extends Activity{

    SharedPreferences sharedPreferences = null; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.starting_point);

        sharedPreferences; = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

这样它应该可以工作......干杯!

于 2013-03-22T22:14:15.050 回答
1

尝试调用getSharedPreferencesonCreate 内部以确保您的 Context 存在。不确定这是否是您异常的原因,但值得一试。

于 2013-03-22T22:14:47.213 回答