在ListView中精确定位EditText

时间:2014-02-01 02:49:10

标签: java android listview android-edittext

我的Android应用程序遇到了问题。当我尝试获取EditText中所有ListView的文本时,我得到一个NullPointerException。我想确切地确定哪个EditText(s)为空或没有值。这是我的代码:

    public void next(View v) {
    EditText MeditText;
    View view = (View)v.getParent();

    MeditText = (EditText) view.findViewById(R.id.editText);
    if (MeditText != null) {
    Intent intent = new Intent(AddManually2.this, DatabaseFiller.class);
    startActivity(intent);
    } else {
    Toast.makeText(getApplicationContext(), "Sorry! Something's not right!", Toast.LENGTH_SHORT).show();
    }
}

public void next(View v){}是单击按钮的方法。该方法始终为else {}。如果我取出if / else方法,我的应用程序崩溃了。这是我的LogCat:

01-31 21:39:12.148  13217-13217/com.example.foodsaver2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.foodsaver2, PID: 13217
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3823)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3818)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.foodsaver2.AddManually2.next(AddManually2.java:139)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3818)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

如果你给我代码用null EditText来解决整个问题,我会很感激,而不仅仅是给我一些用于精确定位EditText的代码。我已经在SO和Google上呆了一段时间,但我找不到解决方案。任何有关此问题的帮助都将受到高度赞赏。

0 个答案:

没有答案