启动后出现NullPointer异常

时间:2014-10-09 09:50:44

标签: android android-intent nullpointerexception

我的过渡时遇到问题。希望有人可以帮助我。

首先,启动画面活动的代码。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    /****** Create Thread that will sleep for 5 seconds *************/        
    Thread background = new Thread() 
    {
        public void run() 
        {
            try 
            {
                // Thread will sleep for 5 seconds
                sleep(5*1000);

                // After 5 seconds redirect to another intent
                if(SharedPreferences.getUserName(getApplication()) != null && SharedPreferences.getUserName(getApplication()).length() == 0)
                {
                    Intent i = new Intent(getBaseContext(), LoginActivity.class);
                    startActivity(i);
                }

                else
                {
                    Intent i = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(i);
                }

                //Remove activity
                finish();

            } 

            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    };

    // start thread
    background.start();

}

接下来,登录活动的代码:

公共类LoginActivity扩展了FragmentActivity {

public static String user_id = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i("LoginActivity", "Start Login Activity");

    int titleId = getResources().getIdentifier("title_activity_login", "id", "android");
    TextView abTitle = (TextView) findViewById(titleId);
    abTitle.setTextColor(Color.WHITE);
}

然后,登录活动开始时启动的片段:

public static class CardSectionFragment extends Fragment 
{
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    Button registerBtn, toggleBtn;
    EditText useridET;
    EditText passwordET;
    EditText dobET;
    Spinner daySpin, mthSpin, yrSpin;
    LinearLayout dobLayout;

    public CardSectionFragment() 
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        recLifeCycle_with_savedInstanceState(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_register,
                container, false);
        registerBtn = (Button) rootView.findViewById(R.id.registerBtn);
        toggleBtn = (Button) rootView.findViewById(R.id.toggleBtn);
        useridET = (EditText) rootView.findViewById(R.id.useridET);
        passwordET = (EditText) rootView.findViewById(R.id.passwordET);

        dobLayout = (LinearLayout) rootView.findViewById(R.id.dobLayout);

        daySpin = (Spinner) rootView.findViewById(R.id.daySpin);
        mthSpin = (Spinner) rootView.findViewById(R.id.mthSpin);
        yrSpin = (Spinner) rootView.findViewById(R.id.yrSpin);

        ArrayAdapter<String> dayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, R.array.date_spin);
        dayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        ArrayAdapter<String> mthAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, R.array.mnth_spin);
        mthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        ArrayAdapter<String> yrAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, R.array.year_spin);
        yrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        daySpin.setAdapter(dayAdapter);
        mthSpin.setAdapter(mthAdapter);
        yrSpin.setAdapter(yrAdapter);

        registerBtn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                ((MainActivity) getActivity()).hideSoftKeyboard();
                // TODO Auto-generated method stub
                // ((MainActivity) getActivity()).registerSuccess("");
                if (registerBtn.getText().toString().equals("Sign Up")) 
                {
                    String dob = daySpin.getSelectedItem().toString() + "-" + mthSpin.getSelectedItem().toString() + "-" + yrSpin.getSelectedItem().toString().substring(Math.max(yrSpin.getSelectedItem().toString().length() - 2, 0));
                    new RegisterUserAsyncTask((LoginActivity) getActivity()).execute(useridET.getText().toString(), passwordET.getText().toString(), dob);
                } 

                else 
                {
                    if (registerBtn.getText().toString().equals("Login")) 
                    {
                        user_id = useridET.getText().toString();
                         new LoginUserAsyncTask((LoginActivity) getActivity()).execute(useridET.getText().toString(),passwordET.getText().toString());

                    }
                }
            }

        });

        toggleBtn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                if (registerBtn.getText().toString().equals("Sign Up"))
                {
                    dobLayout.setVisibility(View.GONE);
                    registerBtn.setText("Login");
                    toggleBtn.setText("Do not have an account? Register here.");
                }

                else 
                {
                    dobLayout.setVisibility(View.VISIBLE);
                    registerBtn.setText("Sign Up");
                    toggleBtn.setText("To begin, login with your account");
                }
            }

        });

        return rootView;
    }

    public void recLifeCycle() {

        String className = getClass().getSimpleName();
        StackTraceElement[] s = Thread.currentThread().getStackTrace();
        String methodName = s[3].getMethodName();

        String msg = className + "." + methodName;

        // Toast.makeText(getActivity(),
        // msg, Toast.LENGTH_SHORT).show();
        Log.i("MYTAG", msg);
    }

    public void recLifeCycle(String note) {

        String className = getClass().getSimpleName();
        StackTraceElement[] s = Thread.currentThread().getStackTrace();
        String methodName = s[3].getMethodName();

        String msg = className + "." + methodName;

        // Toast.makeText(getActivity(),
        // msg, Toast.LENGTH_SHORT).show();
        Log.i("MYTAG", msg + " / " + note);
    }

    public void recLifeCycle_with_savedInstanceState(Bundle state) {

        String stateMsg;
        if (state == null) {
            stateMsg = "savedInstanceState == null";
        } else {
            stateMsg = "savedInstanceState != null";
        }

        String className = getClass().getSimpleName();
        StackTraceElement[] s = Thread.currentThread().getStackTrace();
        String methodName = s[3].getMethodName();

        String msg = className + "." + methodName + " / " + stateMsg;

        // Toast.makeText(getActivity(),
        // msg, Toast.LENGTH_SHORT).show();
        Log.i("MYTAG", msg);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onResume();

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onDestroyView();

    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onDetach();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onPause();
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onStart();
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        recLifeCycle();
        super.onStop();
    }
}

而且,这是错误:(我能找到的最接近错误的东西)

10-09 17:17:01.642: I/LoginActivity(12487): Start Login Activity
10-09 17:17:01.652: D/AndroidRuntime(12487): Shutting down VM
10-09 17:17:01.652: W/dalvikvm(12487): threadid=1: thread exiting with uncaught exception (group=0x42f00500)
10-09 17:17:01.662: E/AndroidRuntime(12487): FATAL EXCEPTION: main
10-09 17:17:01.662: E/AndroidRuntime(12487): java.lang.RuntimeException: Unable to start activity ComponentInfo{nyp.edu.sg.alumnigo/nyp.edu.sg.alumnigo.LoginActivity}: java.lang.NullPointerException
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread.access$600(ActivityThread.java:148)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.os.Looper.loop(Looper.java:150)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread.main(ActivityThread.java:5406)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at java.lang.reflect.Method.invoke(Method.java:525)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at dalvik.system.NativeStart.main(Native Method)
10-09 17:17:01.662: E/AndroidRuntime(12487): Caused by: java.lang.NullPointerException
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at nyp.edu.sg.alumnigo.LoginActivity.onCreate(LoginActivity.java:42)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.Activity.performCreate(Activity.java:5303)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
10-09 17:17:01.662: E/AndroidRuntime(12487): 	... 11 more

我不知道为什么它有一个空指针,请帮忙。 第42行是abTitle.setTextColor(Color.WHITE);在登录活动中btw。

编辑:

这是我的activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />

2 个答案:

答案 0 :(得分:0)

如果您确定view-id,请尝试替换此行:

TextView abTitle = (TextView) findViewById(titleId);

使用:

TextView abTitle = (TextView) getWindow().getDecorView().getRootView().findViewById(titleId);

如果你想改变android标准ActionBar的标题,请使用:

getActionBar().setTitle("your title");

答案 1 :(得分:0)

找到答案: 我应该使用action_bar_title而不是使用title_activity_login。 我假设它是@string资源ID。因此,无法找到它。对带来的麻烦表示抱歉。