I have a custom adapter that extends ArrayAdapter. I want to go to MapActivity when the EMAIL is clicked. If I remove the intent part a toast appears with the correct position of the item clicked. However, if I add the startActivity function, the app crashes (no toast). I removed the irrelevant code such as getting distance and setters.getters. Any help or pointer would be appreciated!
ArrayAdapter code
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.auth.FirebaseAuth;
import java.util.ArrayList;
import java.util.List;
public class TrackAdapter extends ArrayAdapter{
private Context c;
//ArrayLists to save items
public TrackAdapter(Context context, int resource)
{
super(context, resource);
this.c = context;
}
public void add(/*items to be added*/)
{
//add to ArrayLists and super.add()
}
static class RowHolder
{
TextView EMAIL;
TextView DISTANCE;
}
@Override
public int getCount()
{
return this.email.size();
}
@Override
public Object getItem(int position)
{
return this.email.get(position);
}
/*other setters and getters*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
row = convertView;
RowHolder holder;
final int currentPosition = position;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.track_item, parent, false);
holder = new RowHolder();
holder.EMAIL = (TextView) row.findViewById(R.id.track_item_text);
holder.DISTANCE = (TextView) row.findViewById(R.id.track_item_distance);
holder.EMAIL.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(c, "item at position " + currentPosition, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, MapActivity.class);
c.startActivity(intent);
}
});
row.setTag(holder);
}
else
{
holder = (RowHolder) row.getTag();
}
String EM = (String) getItem(position);
holder.EMAIL.setText(EM);
//calculate and display distance
return row;
}
}
I also tried replacing c with c.getApplicationContext()
and v.getContext()
, which also doesn't work.
EDIT- I ran the app on an emulator, and it goes to the MapActivity. However, when ran on an android device connected by USB, the app crashes
EDIT2 - Here is the crash report
--------- beginning of crash
03-29 03:23:42.036 7768-7768/com.example.*******
E/AndroidRuntime: FATAL EXCEPTION: main
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:672)
at android.app.ContextImpl.startActivity(ContextImpl.java:659)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
at com.example.ankit.findyourfellow.TrackAdapter$1.onClick(TrackAdapter.java:127)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Inside your click listener do something like:
holder.EMAIL.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(c, "item at position " + currentPosition, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, MapActivity.class);
//if you want to send data to called activity uncomment next line
// intent.putExtra("extra", "value");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intent);
}
});
It should work :)