简体   繁体   English

NullPointException,二进制计算器应用程序

[英]NullPointException, Binary calculator application

I'm trying to make an android application that takes an inputed decimal number and converts it to binary code. 我正在尝试制作一个使用输入的十进制数字并将其转换为二进制代码的android应用程序。 Right now, I get the following compile error whenever I run the application through the emulator: Edit: more log information and code change, still have the same problem. 现在,每当通过模拟器运行应用程序时,都会出现以下编译错误:编辑:更多日志信息和代码更改仍然存在相同的问题。

07-25 19:46:35.814: E/AndroidRuntime(2248): FATAL EXCEPTION: main
07-25 19:46:35.814: E/AndroidRuntime(2248): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.binaryconverter/com.example.binaryconverter.MainActivity}: java.lang.NullPointerException
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.os.Looper.loop(Looper.java:137)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at java.lang.reflect.Method.invoke(Method.java:511)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at dalvik.system.NativeStart.main(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248): Caused by: java.lang.NullPointerException
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.Activity.findViewById(Activity.java:1839)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at com.example.binaryconverter.MainActivity.<init>(MainActivity.java:30)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at java.lang.Class.newInstanceImpl(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at java.lang.Class.newInstance(Class.java:1319)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-25 19:46:35.814: E/AndroidRuntime(2248):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

This is for a class and I'm having a hard time figuring out a solution. 这是一堂课,我很难解决。

package com.example.binaryconverter;
import android.renderscript.Script.FieldID;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
import java.io.*;
import java.util.*;
import java.math.*;

public class MainActivity extends Activity implements OnClickListener{

    int inputNumber = 0; //inputs the number for the conversion to take place
    int i=0;    //integer to divide by
    int number = 0; //number given by field

    public String toBinary(int decimal) 
    {
        String result = "" ;
        while (decimal > 0) {
            int bit = decimal % 2 ;
            result = "" + bit + result ;
            decimal = decimal / 2 ;
        }

        for (int i=result.length(); i <= 8; i++) {
            result = "0" + result ;
        }

        return result ;
    }

Button calc =  (Button)findViewById(R.id.convert);
Button clear = (Button)findViewById(R.id.clear);


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

    }

    @Override
    public void onClick(View v) 
    {
    TextView display = (TextView)findViewById(R.id.binaryView);
    EditText input = (EditText)findViewById(R.id.inputField);
    int inputs = Integer.parseInt(input.getText().toString());
        calc = (Button) v ;
        if (v.getId() == clear.getId()) {
            display.setText("") ;
            input.setText("") ;
        } else if (v.getId() == calc.getId()) {
            if (input.getText() != null) {
            int value = Integer.parseInt(input.getText().toString()) ;
            display.setText(toBinary(value)) ;
        }
    }
}

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

In the future, please post more lines of the Log so that we can see the exact line where the error occurred. 将来,请发布更多行日志,以便我们可以看到发生错误的确切行。 It should show that further down in Logcat, and that will help you immensely to solve these problems yourself. 它应该在Logcat中进一步说明这一点,这将极大地帮助您自己解决这些问题。

The problem is most likely that you are trying to find Views in your layout before you have inflated them, and you are calling methods on these objects. 问题很可能是您在放大视图之前尝试在布局中查找视图,并且正在这些对象上调用方法。 In these lines: 在这些行中:

EditText input = (EditText)findViewById(R.id.inputField);
int inputs = Integer.parseInt(input.getText().toString());

You are trying to find input and call getText() on it, but input will be null at this point. 您正在尝试查找input并对其调用getText() ,但此时输入将为null。 You must do this after you call setContentView(layout) in onCreate() . onCreate()调用setContentView(layout)之后,必须执行此操作。

Move all of your View initialization and method calls into onCreate() AFTER setContentView() . 将所有View初始化和方法调用移到setContentView()之后的onCreate() setContentView()

EDIT: 编辑:

After the new log that you posted, I can see that it is crashing in findViewById . 在您发布新日志之后,我可以发现它在findViewById崩溃了。 That is because you are calling findViewById as soon as the class is instantiated in these two lines: 这是因为您在这两行中实例化了该类后就立即调用findViewById:

Button calc =  (Button)findViewById(R.id.convert);
Button clear = (Button)findViewById(R.id.clear);

You have to do this only after setContentView has executed in onCreate() . 仅在onCreate()执行了setContentView之后,才需要执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM