Android EditText提供nullpointer

时间:2014-05-13 04:01:34

标签: java android android-edittext

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

这是我的问题开始的地方(第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)

4 个答案:

答案 0 :(得分:1)

您已使用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中是否可用

答案 1 :(得分:0)

检查 userIdtext2.getText()是否为空。如果它不为null,则使用 userIdtext2.getText()。toString()

因为在null对象上使用toString()会导致 NPE

参考:Convert null object to String

答案 2 :(得分:0)

我想你的问题就在这一行

USER_ID = USER_ID_TMP.toUpperCase();

因为user_id_temp在您尝试将其转换为大写字母时不包含任何内容,因为它不包含“{1}}”的原因。尝试使用一些初始输入或对其进行注释然后试试。

答案 3 :(得分:0)

更新您的EditText ID(此处两者都相同)

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

可能是USER_ID_TMP = userIdtext2.getText().toString();

行中的其他问题

此处getText();

中找不到任何内容

首先,您必须setText("");