简体   繁体   中英

NullPointException, Binary calculator application

I'm trying to make an android application that takes an inputed decimal number and converts it to binary code. 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.

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. You must do this after you call setContentView(layout) in onCreate() .

Move all of your View initialization and method calls into onCreate() AFTER setContentView() .

EDIT:

After the new log that you posted, I can see that it is crashing in findViewById . That is because you are calling findViewById as soon as the class is instantiated in these two lines:

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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