I got an error when I try to run my application. I hope someone can point me in the right direction

Here is where my problem starts(Line 73):

USER_ID_TMP = userIdtext2.getText().toString();

Longer code snippet:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);
    Log.i("tabs", "CurrentTab is: 1");

    DB_NAME_AND_PATH = Tab1.this.getDatabasePath(DB_NAME);
    Log.d("Haze", "DB_NAME_AND_PATH: "+DB_NAME_AND_PATH);

    listView = (ListView) findViewById(R.id.listViewer);
    Button copyDbButton = (Button)findViewById(R.id.copydbbutton);
    Button readDbButton = (Button)findViewById(R.id.readdbbutton);
    final EditText userIdtext= (EditText) findViewById(R.id.userIDtextBox);
    EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox);

    if(fileFound){
        Log.d("Haze", "Filefound...........");
      readDbButton.setEnabled(false);
    }else{
      Log.d("Haze", "FileNotfound...........");
      fileFound = checkFileOnCreate(DB_NAME_AND_PATH);
      readDbButton.setEnabled(true);
    }

    USER_ID_TMP = userIdtext2.getText().toString();
    Log.d("Haze", "USER_ID_TMP: "+USER_ID_TMP);
    USER_ID = USER_ID_TMP.toUpperCase();

    final TextWatcher watcher= new TextWatcher() {
      public void afterTextChanged(Editable s) {
            if (userIdtext.getText().toString().equals("")) {
              userIdtext.setBackgroundColor(getResources().getColor(android.R.color.white));
        userIdtext.setTextColor(getResources().getColor(android.R.color.black));
            }

Error message:

05-13 03:43:08.778    2079-2079/com.androidexample.tabbar D/Haze﹕ FileNotfound...........
05-13 03:43:08.800    2079-2079/com.androidexample.tabbar D/AndroidRuntime﹕ Shutting down VM
05-13 03:43:08.800    2079-2079/com.androidexample.tabbar W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-13 03:43:08.828    2079-2079/com.androidexample.tabbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.TabBar}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.Tab1}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.Tab1}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
            at android.widget.TabHost.setCurrentTab(TabHost.java:346)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.androidexample.tabbar.TabBar.onCreate(TabBar.java:43)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.androidexample.tabbar.Tab1.onCreate(Tab1.java:73)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
            at android.widget.TabHost.setCurrentTab(TabHost.java:346)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.androidexample.tabbar.TabBar.onCreate(TabBar.java:43)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
有帮助吗?

解决方案

you have used tab1 layout to set view by using setContentView(R.layout.tab1);

And initize userIdtext2 by using id userIDtextBox from this line EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox);

Then you take value of userIdtext2 by following code

USER_ID_TMP = userIdtext2.getText().toString();

so check if the id is R.id.userIDtextBox is available in layout R.layout.tab1 or not ?

其他提示

Check if your userIdtext2.getText() is null or not. If it is not null, then use userIdtext2.getText().toString()

Because using toString() on null object is known to cause NPE

Reference : Convert null object to String

I guess your problem is in this line

USER_ID = USER_ID_TMP.toUpperCase();

because user_id_temp contains nothing so when you try to convert it to upper case, since it contain nothing that's why it throws exception.Try it with some inital input or comment it and then try.

Update your EditText Id's (Here both are same)

 final EditText userIdtext= (EditText) findViewById(R.id.userIDtextBox1);
 EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox2);

May be other issue in line USER_ID_TMP = userIdtext2.getText().toString();

Here nothing found in getText();

So first you have to setText("");

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top