简体   繁体   English

尝试在Android中添加ClickListener时获取NullPointerException。为什么?

[英]Getting NullPointerException when trying to add ClickListener in Android.. why?

I'm a fresh beginner to Android development and I'm trying to make a unit conversion app. 我是Android开发的新手,我正在尝试制作单位转换应用。 My distance activity is able to lay itself out, but it all goes to hell when I try to add ClickListeners. 我的远程活动可以自行安排,但是当我尝试添加ClickListener时,一切都会陷入困境。 I get a NullPointerException and I have no idea why, my first thought was that I was referencing the wrong Id but it seems like I'm referencing the right places. 我收到NullPointerException,我不知道为什么,我的第一个念头是我在引用错误的ID,但似乎在引用正确的位置。 The app launches to the Main screen where there is a button to open the Distance activity. 该应用程序将启动到主屏幕,其中有一个按钮可打开“距离”活动。 As soon as I try to open Distance, the app crashes. 一旦我尝试打开“距离”,该应用就会崩溃。 Here's Distance: 这是距离:

package org.belliveau.convert;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

    public class Distance extends Activity {

        int inputInInches, output;
        Button inchInput, footInput, yardInput, mileInput, inchOutput, footOutput, yardOutput, mileOutput;
        TextView result;
        EditText inputText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setButtons();
            setContentView(R.layout.activity_distance);
            setClickListeners();
            // Show the Up button in the action bar.
            setupActionBar();
        }

        /**
         * Set up the {@link android.app.ActionBar}, if the API is available.
         */
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.distance, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        private void setButtons(){
            inchInput = (Button) findViewById(R.id.inch);
            footInput = (Button) findViewById(R.id.foot_button);
            yardInput = (Button) findViewById(R.id.yard_button);
            mileInput = (Button) findViewById(R.id.mile_button);

            inchOutput = (Button) findViewById(R.id.inch_post);
            footOutput = (Button) findViewById(R.id.foot_post);
            yardOutput = (Button) findViewById(R.id.yard_post);
            mileOutput = (Button) findViewById(R.id.mile_post);

            result = (TextView) findViewById(R.id.dist_result);
            inputText = (EditText) findViewById(R.id.distance_initial);
        }

        private void setClickListeners(){
            inchInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    inch2inch(input);
                }

            });
            footInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    foot2inch(input);
                }

            });
            yardInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    yard2inch(input);
                }

            });
            mileInput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    int input = Integer.valueOf(inputText.getText().toString());
                    mile2inch(input);
                }

            });
            inchOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    inchDuh();
                }

            });
            footOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputFoot();
                }

            });
            yardOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputYard();
                }

            });
            mileOutput.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    outputMile();
                }

            });
        }

        public void inch2inch(int inch) {
            inputInInches = inch;
        }

        public void foot2inch(int foot) {
            inputInInches = foot * 12;
        }

        public void yard2inch(int yard) {
            int foot = yard * 3;
            inputInInches = foot * 3;

        }

        public void mile2inch(int mile) {
            int yard = mile * 1760;
            int foot = yard * 3;
            inputInInches = foot * 3;

        }

        public void outputFoot() {
            output = inputInInches / 12;
            result.setText(output);
        }

        public void outputYard() {
            int foot = inputInInches / 12;
            output = foot / 3;
            result.setText(output);
        }

        public void outputMile() {
            int foot = inputInInches / 12;
            int yard = foot / 3;
            output = yard / 1760;
            result.setText(output);
        }
        public void inchDuh(){
            output = inputInInches;
            result.setText(output);
        }

and LogCat: 和LogCat:

12-20 21:34:35.674: I/Adreno-EGL(3199): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
12-20 21:34:35.674: I/Adreno-EGL(3199): OpenGL ES Shader Compiler Version: 17.01.10.SPL
12-20 21:34:35.674: I/Adreno-EGL(3199): Build Date: 10/30/13 Wed
12-20 21:34:35.674: I/Adreno-EGL(3199): Local Branch: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Remote Branch: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Local Patches: 
12-20 21:34:35.674: I/Adreno-EGL(3199): Reconstruct Branch: 
12-20 21:34:35.744: D/OpenGLRenderer(3199): Enabling debug mode 0
12-20 21:34:38.474: D/AndroidRuntime(3199): Shutting down VM
12-20 21:34:38.474: W/dalvikvm(3199): threadid=1: thread exiting with uncaught exception (group=0x4166dd28)
12-20 21:34:38.476: E/AndroidRuntime(3199): FATAL EXCEPTION: main
12-20 21:34:38.476: E/AndroidRuntime(3199): Process: org.belliveau.convert, PID: 3199
12-20 21:34:38.476: E/AndroidRuntime(3199): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.belliveau.convert/org.belliveau.convert.Distance}: java.lang.NullPointerException
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.os.Looper.loop(Looper.java:137)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.main(ActivityThread.java:5083)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at java.lang.reflect.Method.invoke(Method.java:515)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at dalvik.system.NativeStart.main(Native Method)
12-20 21:34:38.476: E/AndroidRuntime(3199): Caused by: java.lang.NullPointerException
12-20 21:34:38.476: E/AndroidRuntime(3199):     at org.belliveau.convert.Distance.setClickListeners(Distance.java:82)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at org.belliveau.convert.Distance.onCreate(Distance.java:28)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.Activity.performCreate(Activity.java:5260)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
12-20 21:34:38.476: E/AndroidRuntime(3199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
12-20 21:34:38.476: E/AndroidRuntime(3199):     ... 12 more

Inverse these calls 反向调用

setButtons();
setContentView(R.layout.activity_distance);

In other words, they should be 换句话说,它们应该是

setContentView(R.layout.activity_distance);
setButtons();

You're calling setButtons() which tries to get views from an unitialized UI and therefore 您正在调用setButtons() ,尝试从统一的UI获取视图,因此

inchInput = (Button) findViewById(R.id.inch); // returns null

sets inchInput to null . inchInput设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试添加导航抽屉时出现NullPointerException(Android) - NullPointerException when trying to add a Navigation Drawer (Android) 为什么在尝试将实体保存到列表时会收到 NullPointerException? - Why am i getting a NullPointerException when trying to save an entity to a list? 在 Java 中,为什么我在尝试显示 FileNotFoundException 时会收到 NullPointerException? - In Java, why am I getting a NullPointerException when trying to display a FileNotFoundException? 为什么我在尝试读取文件时收到 NullPointerException? - Why am I getting a NullPointerException when trying to read a file? 为什么在尝试填充数组时会出现NullPointerException? - Why, when trying to populate an array, am I getting a NullPointerException? java:尝试添加到对象列表并获取nullPointerException - java: Trying to add to an object list and getting nullPointerException 在 Selenium 中尝试 pom 时出现 NullPointerException - Getting a NullPointerException when trying a pom in Selenium 尝试在画布上绘制时出现nullpointerexception错误 - Getting a nullpointerexception error when trying to draw to canvas 尝试创建ArrayList时获取NullPointerException - Getting a NullPointerException when trying to create ArrayList 尝试使用SQL插入时获取NullPointerException - Getting a NullPointerException when trying to insert using SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM