一个按钮的多项任务

时间:2017-01-11 14:26:42

标签: android android-intent android-edittext imageview spinner

 EditText e = (EditText)findViewById(Edit_current);
    Spinner s = (Spinner)findViewById(R.id.edit_destination);
    navigatebut = (Button) findViewById(R.id.navigate);
    final String current = e.getText().toString();
    final String dest = s.getSelectedItem().toString();
    navigatebut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to do your drawable
            if (current.equals("Entrance") && dest.equals("Vegetables")) {
                layout.setImageResource(R.drawable.entrance_vege);;
                //do something
            }

        }
    });
}}

How it looks like

当我运行它时,它有一个错误java.lang.nullpointerexception。 我可以知道它的来源吗?

E / AndroidRuntime:致命异常:主要                   处理:com.sp.ez_mart,PID:17820                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.sp.ez_mart / com.sp.ez_mart.NavigateImage}:java.lang.NullPointerException                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:136)                       在android.app.ActivityThread.main(ActivityThread.java:5017)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                       at dalvik.system.NativeStart.main(Native Method)                    引起:java.lang.NullPointerException                       在com.sp.ez_mart.NavigateImage.onCreate(NavigateImage.java:31)                       在android.app.Activity.performCreate(Activity.java:5231)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:136)                       在android.app.ActivityThread.main(ActivityThread.java:5017)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                       在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

堆栈跟踪清楚地告诉您错误在哪里@ NavigateImage.java Line#31

查看代码,您似乎在以下行中传递了错误的资源ID 。可能是您打算使用R.id.edit_destination

EditText e = (EditText)findViewById(Edit_current);

所以,我怀疑,它正在崩溃

final String current = e.getText().toString();

因为找不到EditText e

注意:将来作为最佳做法,请使用描述性变量名称。