0

I'm getting logcat errors where I casted an anonymous inner class for onKeyListener. As far as I know (I'm still learning all the ropes) there's nothing wrong with the line that's referenced here's the full error log for the session:

02-23 16:28:45.806: E/AndroidRuntime(482): FATAL EXCEPTION: main
02-23 16:28:45.806: E/AndroidRuntime(482): java.lang.RuntimeException: Unable to start activity ComponentInfo{omaxwell.CS211D.PennyConverter/omaxwell.CS211D.PennyConverter.PennyConverterActivity}: java.lang.NullPointerException
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.os.Looper.loop(Looper.java:132)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread.main(ActivityThread.java:4025)
02-23 16:28:45.806: E/AndroidRuntime(482):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 16:28:45.806: E/AndroidRuntime(482):  at java.lang.reflect.Method.invoke(Method.java:491)
02-23 16:28:45.806: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-23 16:28:45.806: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-23 16:28:45.806: E/AndroidRuntime(482):  at dalvik.system.NativeStart.main(Native Method)
02-23 16:28:45.806: E/AndroidRuntime(482): Caused by: java.lang.NullPointerException
02-23 16:28:45.806: E/AndroidRuntime(482):  at omaxwell.CS211D.PennyConverter.PennyConverterActivity.onCreate(PennyConverterActivity.java:31)//onKeyListener reference
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-23 16:28:45.806: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)

And here's my Activity code:

package omaxwell.CS211D.PennyConverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class PennyConverterActivity extends Activity
{

    EditText et;
    TextView tv;

    int cents;
    int remaining;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
    String result;

    @Override

    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        setContentView(R.layout.main);

        et.setOnKeyListener(new View.OnKeyListener() //Line referenced in LogCat
        {   
            @Override
            public boolean onKey(View v, int KeyCode, KeyEvent event) 
            {
                if((event.getAction() == KeyEvent.ACTION_DOWN) && (KeyCode == KeyEvent.KEYCODE_ENTER))
                { 
                    result = et.getText().toString(); 
                    return (true);
                }
                return (true);
            }
        });

        cents = Integer.parseInt(result);

        quarters = cents / 25;
        tv.setText(R.string.quarters);
        remaining = cents % 25;

        dimes = remaining / 10;
        tv.setText(R.string.dimes);
        remaining = remaining % 10;

        nickels = remaining / 5;
        tv.setText(R.string.nickles);

        pennies = remaining % 5;
        tv.setText(R.string.pennies);

    }
}

When I run the app in the emulator it doesn't launch automatically and when I go to the app drawer to launch it, it crashes on launch.

  • NullPointerException is a simple error, please check your code carefully, may be some where the var can't init, the var `et` inited? – idiottiger Feb 24 '12 at 01:27

1 Answers1

0

You never initialized et. I suppose you're missing

et = (EditText) findViewById(R.id.my_edit_text);

right above that line.

EboMike
  • 76,846
  • 14
  • 164
  • 167