使用EditText的Android nullPointerException

时间:2014-03-19 14:32:02

标签: java android nullpointerexception android-edittext

在我的应用程序中,我正在尝试操作EditText元素,但每次我尝试这样做时,它都会向我抛出nullPointerException。我不知道为什么会这样做,因为它似乎只发生在这个EditText,docTitle上,而不是我的任何其他人。

以下是抛出异常的java类的一部分:

 public class MainActivity extends FragmentActivity implements RichText.EditTextImeBackListener, OnTouchListener, OnClickListener, TextWatcher{
     private String[] drawerListViewItems1;
     private String[] drawerListViewItems2;
     private ArrayList<Editable> undoStrings;

     private int undoIndex;

     //this is the EditText that is giving me trouble, it is defined down in onCreate
     public EditText docTitle;

     private int styleStart;

     private ListView drawerListView1;
     private ListView drawerListView2;

     private RelativeLayout formatBar;

     private DrawerLayout drawerLayout;
     private ActionBarDrawerToggle actionBarDrawerToggle;
     private RichText richText;
     private SaveFragment saveFragment;

     private ToggleButton boldButton;
     private ToggleButton emButton;
     private ToggleButton uButton;
     private ToggleButton strikeButton;

     private boolean keyboardShown;
     private boolean alreadyShown;
     private boolean isMainContent;

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

         //finds the left drawer
         drawerListView1 = (ListView) findViewById(R.id.left_drawer);
         //finds right drawer
         drawerListView2 = (ListView)findViewById(R.id.right_drawer);

         //loads the first drawer
         loadDrawer1();

         //loads the second drawer
         loadDrawer2();

         //finds the drawer layout
         drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

         //assign ActionBarDrawerToggle
         actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, 
                 R.string.drawer_open, R.string.drawer_close);

         //sets the drawer listener
         drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
             @Override
             public void onDrawerOpened(View drawerLayout){
                 //hides format bar when a drawer is opened
                 if(keyboardShown){
                     hideFormatBarAnim();
                 }
             }

             @Override
             public void onDrawerClosed(View drawerLayout){
                 //shows format bar when closed
                 showFormatBar();
             }
         });

         //enable and show "up" arrow
         getActionBar().setDisplayHomeAsUpEnabled(true); 

         // just styling option for the drawer layout
         drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

         //item click listener for drawer items
         drawerListView1.setOnItemClickListener(new DrawerItemClickListener());
         drawerListView2.setOnItemClickListener(new DrawerItemClickListener());

         isMainContent= true;
         alreadyShown=false;
         keyboardShown=false;

         undoIndex=0;

         undoStrings = new ArrayList<Editable>();

         //assigns formatBar to its XML layout 
         formatBar = (RelativeLayout)findViewById(R.id.formatBar);

         //assigns docTitle to its XML layout
         docTitle = (EditText)findViewById(R.id.docTitle);
         //This line here throws the error
         docTitle.setText("Hello");

         //defines save fragment
         saveFragment = new SaveFragment();

         //assigns richText to its XML layout and adds a TextChangeListener
         richText= (RichText)findViewById(R.id.edit_text);
         richText.addTextChangedListener(this);

         //assigns boldButton to its XML layout and adds an OnClickListener
         boldButton=(ToggleButton)findViewById(R.id.bold);
         boldButton.setOnClickListener(this);

         //assigns emButton to its XML layout and adds an OnClickListener
         emButton=(ToggleButton)findViewById(R.id.ital);
         emButton.setOnClickListener(this);

         //assigns uButton to its XML layout and adds an OnClickListener
         uButton=(ToggleButton)findViewById(R.id.underline);
         uButton.setOnClickListener(this);

         //assigns strikeButton to its XML layout and adds an onClickListener
         strikeButton=(ToggleButton)findViewById(R.id.strike);
         strikeButton.setOnClickListener(this);

         //initiates sofKeyboardHook
         softKeyboardHook();

         //creates the default saving directory
         Doc.makeDefaultDir();
         lookForFiles();
     }

以下是与之相关的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="#1E772F"
    android:layout_marginTop="350dp">

        <EditText
            android:id="@+id/docTitle"
            android:text="DefaultDocName"
            android:paddingTop="20dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"/>

        <Button 
            android:id="@+id/save"
            android:layout_height="wrap_content"
            android:layout_width="150dp"
            android:text="Save"
            android:layout_below="@+id/docTitle"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:onClick="onSaveDoneClick"/>
        <Button
            android:id="@+id/cancel"
            android:layout_height="wrap_content"
            android:layout_width="150dp"
            android:text="Cancel"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/docTitle"
            android:layout_toRightOf="@+id/save"
            android:onClick="cancelSave"/>


    </RelativeLayout>
</ScrollView>

继续发出错误的日志:

03-19 09:48:50.246  12996-12996/com.noahl98.jotmobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.noahl98.jotmobile, PID: 12996
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noahl98.jotmobile/com.noahl98.jotmobile.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$700(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5083)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.noahl98.jotmobile.MainActivity.onCreate(MainActivity.java:133)
            at android.app.Activity.performCreate(Activity.java:5260)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.access$700(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5083)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您要膨胀的布局称为&#39; activity_main.xml&#39;它似乎包含许多抽屉和其他不在你附加的布局文件中的东西。您确定不会混淆布局文件。

基本上看起来你没有'docTitle&#39;在&#39; activity_main.xml&#39;文件是这个原因

//assigns docTitle to its XML layout
docTitle = (EditText)findViewById(R.id.docTitle);
//This line here throws the error
docTitle.setText("Hello");

为您提供&#39; docTitle = null&#39;

答案 1 :(得分:0)

问题是你在onCreate函数中加载的布局不是具有editText的布局,

你可以检查:

setContentView(R.layout.activity_main);

这个activity_main布局是您发布的那个吗?

因为稍后在onCreate方法中使用的是drawerListView1,它不在您粘贴的XML中,而且不是null。

请确保:您正在使用的视图应该在您正在加载的布局中