1

在这个程序中,我实现了滑动库。实现这个库,我正在尝试创建一个MenuActivity类来处理所有菜单项滑块选项、onClick 侦听器等。现在该类看起来像这样:

public class MenuActivity extends SlidingActivity{

    SharePrefManager sm = new SharePrefManager(MenuActivity.this);
    RelativeLayout ask_a_question, life_prediction;
    TextView aries,taurus,gemini,cancer,leo,virgo,libra,scorpio,sagittarius,capricon,aquarius,pisces;
    String isLoggedIn; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setBehindContentView(R.layout.menu);

        ask_a_question = (RelativeLayout)findViewById(R.id.ask_a_question);

        HashMap<String, String> checkLogin = new HashMap<String, String>();
        checkLogin = sm.getuserDetails();
        isLoggedIn = checkLogin.get(SharePrefManager.KEY_ISLOGEDIN);

        ask_a_question.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (isLoggedIn.equals(0))
                {
                    Toast.makeText(getApplicationContext(), "You must be logged in", Toast.LENGTH_SHORT).show();
                }

                else
                {
                    Intent i = new Intent("com.example.btshome.ASKPAGE");
                    startActivity(i);
                }

            }
        });


    }


}

此类基本上是实现库并设置滑动菜单项和 onClick 事件处理程序。这个类也在访问 Preferences,以检查用户是否登录。

在我的MainActivity类上,我正在扩展该类并尝试从中获取功能。

public class MainActivity extends MenuActivity{

    Button login;

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


        login = (Button)findViewById(R.id.log_in_button);
        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            startActivity(new Intent("com.example.btshome.LOGINACTIVITY"));
            }
        });
    }
}

在以下实现中,发生以下错误:

04-24 10:46:45.081: E/AndroidRuntime(1956): FATAL EXCEPTION: main
04-24 10:46:45.081: E/AndroidRuntime(1956): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.btshome/com.example.btshome.MainActivity}: java.lang.NullPointerException
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.os.Looper.loop(Looper.java:137)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at dalvik.system.NativeStart.main(Native Method)
04-24 10:46:45.081: E/AndroidRuntime(1956): Caused by: java.lang.NullPointerException
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at com.example.btshome.library.SharePrefManager.<init>(SharePrefManager.java:35)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at com.example.btshome.MenuActivity.<init>(MenuActivity.java:17)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at com.example.btshome.MainActivity.<init>(MainActivity.java:14)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at java.lang.Class.newInstanceImpl(Native Method)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at java.lang.Class.newInstance(Class.java:1319)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
04-24 10:46:45.081: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
04-24 10:46:45.081: E/AndroidRuntime(1956):     ... 11 more

那么这里出了什么问题?是MenuActivity类吗?我是否以不同的方式解决这个问题?

4

0 回答 0