尝试运行应用程序时出现错误。
我希望有人能指出我正确的方向

这是我的问题开始的地方(第73行):

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


较长的代码段:

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));
            }


错误信息:

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)

最佳答案

您已使用tab1布局通过以下方式设置视图
setContentView(R.layout.tab1);

然后在此行中使用id userIDtextBox初始化userIdtext2
EditText userIdtext2 =(EditText)findViewById(R.id.userIDtextBox);

然后,通过以下代码获取userIdtext2的值

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

所以检查id是否为R.id.userIDtextBox在布局R.layout.tab1中是否可用?

09-11 11:03