Reputation: 47
I am trying to use a listview as a menu and have it fire off an activity when the user taps (this is on google glass) I have passed over the context, but for some reason the null pointer exception keeps popping up. Was wondering why this was.
Code:
public class HeadListView extends ListView implements SensorEventListener {
private static final float INVALID_X = 10;
private Sensor mSensor;
private int mLastAccuracy;
private SensorManager mSensorManager;
private float mStartX = INVALID_X;
private static final int SENSOR_RATE_uS = 200000;
private static final float VELOCITY = (float) (Math.PI / 180 * 2); // scroll one item per 2°
public int location;
Context myContext;
public HeadListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
myContext=context;
init();
}
public int getLocation()
{
return location;
}
public void setLocation(int location)
{
this.location = location;
}
public HeadListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HeadListView(Context context) {
super(context);
init();
}
public void init() {
mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
public void activate() {
if (mSensor == null)
return;
mStartX = INVALID_X;
mSensorManager.registerListener(this, mSensor, SENSOR_RATE_uS);
}
public void deactivate() {
mSensorManager.unregisterListener(this);
mStartX = INVALID_X;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
mLastAccuracy = accuracy;
}
public boolean onKeyDown(int keycode, KeyEvent event)
{
if (keycode == KeyEvent.KEYCODE_DPAD_CENTER)
{
// user tapped touchpad, should get and store the idd ItemsDto, and the byteArray
try
{
Log.e("Position: ", String.valueOf(location));
//declare intent
if(location==0)
{
Intent intn_test = new Intent("de.tud.ess.ANACTIVITY");
myContext.startActivity(intn_test);
}
}
catch (Exception e)
{
Log.e("MenuScreen:onCreate", e.toString());
}
//overridePendingTransition(R.layout.fade_in, R.layout.fade_out); //fade in/out transition
return true;
}
if (keycode == KeyEvent.KEYCODE_BACK)
{
//goes back to the previous activity, in this case the scanner
return true;
}
return false;
}
The section that throws the null pointer exception is the onKeyDown. This is confusing to me because I (thought i correctly) declared the activity in the manifest.
<activity
android:name="de.tud.ess.AnActivity"
android:label="@string/title_activity_an" >
<intent-filter>
<action android:name="de.tud.ess.ANACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My only thought was that the listview is operating on a separate thread and I might have to use a run on ui function to get the intent to fire off the activity correctly. Any help would be appreciated.
Upvotes: 0
Views: 46
Reputation: 67189
Two of your constructors do not assign a value to myContext
. If either of those constructors are the ones that Android uses to create your View, myContext
will be null and cause a NullPointerException in onKeyDown()
.
You probably want to assign a value to myContext
in all three of your constructors, so you should update these two:
public HeadListView(Context context, AttributeSet attrs) {
super(context, attrs);
myContext = context;
init();
}
public HeadListView(Context context) {
super(context);
myContext = context;
init();
}
Upvotes: 1