I will find a way to get a selected item in my list view and then to cast in my object type, but i get an error i think is to big to see whats is wrong. Can you help me ?
My code :
mListMenu = (ListView) findViewById(R.id.listView_tracks);
mListMenu.setAdapter(new TracksListAdapter(this, TrackManager.getAllTrackFromTel(new DataBaseHelper(this))));
mListMenu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1,
int position, long arg3) {
Track selectedItem = (Track) adapter.getAdapter().getItem(position);
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putLong("trackselected",selectedItem.getTrackid());
intent.putExtras(bundle);
//Envoi du resultat à l'origine
setResult(RESULT_OK, intent);
finish();
}
});
I get this error :
FATAL EXCEPTION: main
java.lang.ClassCastException: java.lang.Integer
at com.milesbox.sport.tracker.ListTracksActivity$1.onItemClick(ListTracksActivity.java:44)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3746)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
Thank you FD_, when i say it was too big to see what is wrong, this is my method(getItem) that return an item that was wrong (copy paste too quick).
Related
I have a AsyncTask in a Service. I send an ArrayList as broadcast from the AsyncTask.
When I get the ArrayList in onReceive() I get a NullpointerException.
This is how I send the ArrayList.
transits_list = new ArrayList<Transit>();
transits_list.add(trs);
Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);
The Transit class implements Serializable.
Receiving the ArrayList
#Override
public void onReceive(Context context, Intent intent) {
ArrayList<Transit> myList;
Bundle extra = getIntent().getBundleExtra("extra");
ArrayList<Transit> transArrayListFromBroadCast = (ArrayList<Transit>) extra.getSerializable("transArray");
System.out.print("transArrayListFromBroadCast "+transArrayListFromBroadCast);
}
I get NullpointerException in this line:
ArrayList<Transit> transArrayListFromBroadCast = (ArrayList<Transit>) extra.getSerializable("transArray");
The exception from log:
FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=arrayList flg=0x10 } in com.prematix.tollsystem.avcc.AvccActivity$ArrayListReceiver#42003268
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.prematix.tollsystem.avcc.AvccActivity$ArrayListReceiver.onReceive(AvccActivity.java:271)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
Your method of getting the Intent is incorrect. I believe your BroadcastReceiver is in an activity class since, you are calling getIntent(). However, getIntent() will get you the Intent supplied to the activity and not the receiver. The Intent for receiver is provided to the method onReceive() itself. Make the following changes to your code:
Adding extra:
Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);
Getting Extra:
#Override
public void onReceive(Context context, Intent intent) {
ArrayList<Transit> myList;
Bundle extra = intent.getBundleExtra("extra");
ArrayList<Transit> transArrayListFromBroadCast = (ArrayList<Transit>) extra.getSerializable("transArray");
// System.out.print("transArrayListFromBroadCast "+transArrayListFromBroadCast);
}
I'm using the ListViewAnimation library and been trying to do the add-item animation for my ListView.
In order to implement such animation, their documentation says that I should do this:
To use this functionality, simply let your adapter implement Insertable, and call one of the insert methods on the DynamicListView:
MyInsertableAdapter myAdapter = new MyInsertableAdapter(); // MyInsertableAdapter implements Insertable
mDynamicListView.setAdapter(myAdapter);
mDynamicListView.insert(0, myItem); // myItem is of the type the adapter represents.
So here's what I did:
I had my Listview be a DynamicListView(I'm using ButterKnife) and set it to use my adapter
#InjectView(R.id.flow_list)
DynamicListView mFlowList;
mFlowListAdapter = new FlowListAdapter(getActivity(), mItems);
mFlowList.setAdapter(mFlowListAdapter);
Made my adapter implement the 'Insertable' interface and overridden its abstract method:
#Override
public void add(int i, #NonNull Object o) {
Item newItem = (Item)o;
mList.add(Item);
notifyDataSetChanged();
}
Call insert() on my ListView
#OnClick(R.id.done)
void addItem() {
...
mFlowList.insert(0,item);
}
But when I ran my app and clicked that done button that triggers the insert() of my ListView I got a NullPointerException saying:
09-15 11:51:41.046 14660-14660/com.jezer.MyApp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.nhaarman.listviewanimations.itemmanipulation.animateaddition.AnimateAdditionAdapter$HeightUpdater.onAnimationUpdate(AnimateAdditionAdapter.java:352)
at com.nineoldandroids.animation.ValueAnimator.animateValue(ValueAnimator.java:1178)
at com.nineoldandroids.animation.ValueAnimator.animationFrame(ValueAnimator.java:1139)
at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:545)
at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)
at com.nineoldandroids.animation.AnimatorSet.start(AnimatorSet.java:502)
at com.nhaarman.listviewanimations.itemmanipulation.animateaddition.AnimateAdditionAdapter.getView(AnimateAdditionAdapter.java:318)
at android.widget.AbsListView.obtainView(AbsListView.java:2232)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15775)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15775)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15775)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15775)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15775)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2193)
at android.view.View.measure(View.java:15775)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2212)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
I'm not getting this exception when I commented out the notifyDataSetChanged() on the add() method. However that's the only way I know to refresh my listview and hopefully see an addition animation to it.
Are you perhaps doing this:
convertView = inflater.inflate(R.layout.cell, null);
Instead of this? convertView = inflater.inflate(R.layout.page_cell, parent, false);
For ~20% of my user-base, they are getting a Unable to start activity ComponentInfo{..} : java.lang.NullPointerExceptionFATAL Exception. I monitor the exceptions remotely via Crashlytics, so I have the stacktrace. The only problem is, there is no line number.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.falk.fietsroutes.app/com.falk.fietsroutes.app.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.access$800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
at com.falk.fietsroutes.app.SearchResultsFragment.onCreateView()
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
at android.app.Activity.performCreate(Activity.java:5392)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.access$800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(NativeStart.java)
I've read many reports from people with the same error, which all came down to something in their code being null (ofcourse, because it's a nullpointer). But I can't track it down because I cannot reproduce it (I only have one device which seems to work fine), and the stack trace doesn't help.
At first sight I couldn't find anything that could be null in SearchResultsFragment.onCreateView() (because that is where the NPE gets thrown right?)
If any of you could point me in the right direction or know how I could get the line number where the exception, it would be appreciated!
Here is the code of SearchResultsFragment.onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set orientation
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
// Get the rootview in the fragment
View rootView = inflater.inflate(R.layout.fragment_search_results, container, false);
// Get settings instance
mSettings = Settings.getInstance();
// Init the mRoutes arraylist
mRoutes = new ArrayList<Route>();
// Get the gridview and set the adapters (animation adapter and the gridview adapter)
GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
SearchResultCardsAdapter searchAdapter = new SearchResultCardsAdapter(getActivity(), inflater);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(searchAdapter);
swingBottomInAnimationAdapter.setAbsListView(gridView);
swingBottomInAnimationAdapter.setInitialDelayMillis(300);
mAdapter = swingBottomInAnimationAdapter;
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("Grid item click", "Clicked!");
Route route = mRoutes.get(i);
Intent routeDetailIntent = new Intent(getActivity(), RouteDetailActivity.class);
routeDetailIntent.putExtra("routeId", route.id);
// Analytics
Tracker t = ((AppDelegate) getActivity().getApplication()).getTracker(
AppDelegate.TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory("Routes")
.setAction("View")
.setLabel(Integer.toString(route.id))
.build());
startActivity(routeDetailIntent);
}
});
gridView.setAdapter(mAdapter);
// Set the infinite scroll listener
gridView.setOnScrollListener(new EndlessScrollListener() {
#Override
public void onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
loadMoreRoutes(page, totalItemsCount);
// or customLoadMoreDataFromApi(totalItemsCount);
}
});
// Show the progressbar
mProgressBar = (SmoothProgressBar)rootView.findViewById(R.id.search_results_progressbar);
mProgressBar.progressiveStart();
// Inflate the layout for this fragment
return rootView;
}
Maybe others will benefit from my mistake. The problem was that I used pro-guard and Android Studio to sign my Android App, and I had to keep-attributes some properties so Crashlytics could read the line number and class name.
-keepattributes SourceFile,LineNumberTable
Via
I have a TO DO List, and when I check the checkbox in one Activity I want the CheckBox and the text that is next to the Checkbox to go to another Activity. this is my Adapter:
public class MyItemAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyItemAdapter(Context context, String[] values) {
super(context, R.layout.item_list, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_list, parent, false);
CheckBox textView = (CheckBox) rowView.findViewById(R.id.checkBox1);
textView.setText(values[position]);
return rowView;
}
public static SparseBooleanArray getCheckedItemPositions() {
// TODO Auto-generated method stub
return null;
}
public static void remove(Object pos) {
// TODO Auto-generated method stub
}
public void changeData(String[] items) {
// TODO Auto-generated method stub
}
public static void setAdapter(MyItemAdapter mAdapter) {
// TODO Auto-generated method stub
}
This is my First Activity code:
final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FinishedItems.class);
intent.putExtra("check", items);
Bundle extras = new Bundle();
extras.putString("status", "Data Recived");
intent.putExtras(extras);
startActivity(intent);
}
});
}
This is my Second Activity code:
Intent intent = getIntent();
String check = intent.getStringExtra("check");
((CheckBox)findViewById(R.id.checkBox1)).setText(check);
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();
And Finaly this is my Log cat:
02-18 10:33:24.779: D/AndroidRuntime(1575): Shutting down VM
02-18 10:33:24.779: W/dalvikvm(1575): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 10:33:24.789: E/AndroidRuntime(1575): FATAL EXCEPTION: main
02-18 10:33:24.789: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Stanton.quicktodolist/com.Stanton.quicktodolist.MainActivity}: java.lang.NullPointerException
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.os.Looper.loop(Looper.java:130)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 10:33:24.789: E/AndroidRuntime(1575): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 10:33:24.789: E/AndroidRuntime(1575): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 10:33:24.789: E/AndroidRuntime(1575): at dalvik.system.NativeStart.main(Native Method)
02-18 10:33:24.789: E/AndroidRuntime(1575): Caused by: java.lang.NullPointerException
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 10:33:24.789: E/AndroidRuntime(1575): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 10:33:24.789: E/AndroidRuntime(1575): ... 11 more
Please Help me
This line of your stacktrace
02-18 10:33:24.789: E/AndroidRuntime(1575): at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)
points you to the error. Line 70 in the MainActivity causes a null pointer. It is often helpful to study and understand stacktraces. If you cannot figure it out yourself, show us the onCreate method of the MainActivity.
I believe that you replace the intent extra by the bundle extra just try this code and let me know pelase
final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FinishedItems.class);
intent.putExtra("check", items);
//Bundle extras = new Bundle();
// extras.putString("status", "Data Recived");
intent.putExtras("status", "Data Recived");
startActivity(intent);
}
});
}
And the int the second activity
Intent intent = getIntent();
String check = intent.getStringExtra("check");
((CheckBox)findViewById(R.id.checkBox1)).setText(check);
// Bundle bundle = intent.getExtras();
String status = intent.getStringExtra("status");
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();
Hello I am currently getting error on some of my apps on android market :
This is the stacktrace :
java.lang.NullPointerException
at ****.****.MainActivity.onListItemClick(MainActivity.java:110)
at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3513)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
at dalvik.system.NativeStart.main(Native Method)
The code part where it goes wrong :
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
mMediaPlayer.stop();
Sound s = (Sound) l.getItemAtPosition(position);
mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId());
mMediaPlayer.start();
}
Mostly of the time it's working (playing sounds) but sometimes it gives a nullpointerexception and I don't know why maybe something with the MediaPlayer or super.onListItemClick() ?
Thanks in advance
Probably s is null? Can you reproduce/debug?
could you provide the code of getItemAtPosition(position) of your list adapter implementation. and may wrap
if (s != null) {
mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId());
mMediaPlayer.start();
}
edit. hmm i was on the wrong line. you should wrap starting the media player with a null check because if you read the doc:
Returns
a MediaPlayer object, or null if creation failed
And then try to find out why creation fails.