ListViewAnimation insert() NullPointerException on notifyDataSetChanged() - java

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);

Related

My AsyncTask in another class is causing NullPointerException when it is called in this class

I am quite new to android development and I am facing a NullPointerException when I am trying to call an AsyncTask which is an inner class in another class.
I believe it is the way I am instantiating it but this is what I have:
// This onClick is in my adapter class - which is a separate Java File and class
#Override
public void onClick(View v) {
UploadRes uploadRes = new UploadRes();
UploadRes.UploadResConfirm uploadResConfirm = uploadRes.new UploadResConfirm(v.getContext());
uploadResConfirm.execute(fileName, filePath);
}
public class UploadResConfirm extends AsyncTask<String, String, String> {
Dialog dialog;
Context context;
public UploadResConfirm(Context context){
this.context = context;
}
#Override
protected void onPreExecute(){
dialog = new Dialog(UploadRes.this);
dialog.setTitle("Currently uploading");
dialog.show();
}
I believe it has got something to do with the dialog box itself being instantiated in the AsyncTask class.
The error stack trace on Logcat - its the Dialog, I think its in the wrong place...
java.lang.NullPointerException
at codeman.androapp.UploadRes$UploadResConfirm.onPreExecute(UploadRes.java:246)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Some help if any would be much obliged.
Instead of dialog = new Dialog(UploadRes.this);
Try
dialog = new Dialog(context);
your are passing a wrong context to create dialog.

android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

There are other related issues on this but they do not address my situation (their error code has to do with some sort of recycling and/or bad cast calls by client code).
My situation is more complex.
I have some code where the user can pull up a library of photos.
The thing is, its working just fine on 7 phones that I have, all running API's 19+.
However I have a Google Nexus 4.3 that's running API 17. And I get this crash log which has none of my code, only library code. If you can advise how I might be able to code a work around I'd be all ears:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:1042)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Update
My offending class is this:
//Special class that works with the UIL
//The holder gets re-used and gets the new properties from this
static class ReusableGridViewView extends FrameLayout
{
public ImageView imageView;
public ProgressBar progressBar;
public ReusableGridViewView(Context context)
{
super(context);
setLayoutParams(new LayoutParams((int)(0.325* ViewHelper.screenWidthPX(context)),(int)(0.325* ViewHelper.screenWidthPX(context))));
imageView = new ImageView(context);
progressBar = new ProgressBar(context);
addView(imageView);
addView(progressBar);
}
}
Im using the Ultimate Image Loader library and this is the code that uses the above class.
//I do NOT call this directly, the listview decides when re use a convert view and passes it accordingly
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View view = convertView;
if (view == null)
{
view = new ReusableGridViewView(parent.getContext());
holder = new ViewHolder();
assert view != null;
holder.imageView = ((ReusableGridViewView)view).imageView;
holder.progressBar = ((ReusableGridViewView)view).progressBar;
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
//System.out.println(" " + holder + " ||| " + holder.imageView);
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener()
{
#Override
public void onLoadingStarted(String imageUri, View view)
{
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason)
{
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener()
{
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total)
{
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
//set all as unselected
((ReusableGridViewView) view).removeSelectionBorder();
for(int i = 0; i < SELECTED_IMAGES.length; i++)
{
if(SELECTED_IMAGES[i] == position)
{
//set cell to selected
((ReusableGridViewView) view).addSelectionBorder();
}
}
return view;
}
So it turns out that on API < 17 FrameLayout can't be cast correctly to AbsListView params.
What I did was set AbsListView LayoutParams instead of FrameLayout params:
setLayoutParams(new AbsListView.LayoutParams(100,100));
I'm not sure if this is going to help you or what, but this crash used to happen for me once I was using AndroidSlidingUpPanel and adding a view inside of its content layout programmatically. After searching and trying many different solutions I came up with this solution; I added the following layout as the first child of the content layout and the crash was solved.
<android.widget.AbsListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent" />

Filling a Spinner from DataBase android

I'm working on a project in which i have to get some informations from the server save them in database using ormlite and after i want to display those informations on a spinner,
So to get the informations from the database to the spinner i called this method:
public Dao<ServiceProviderCompanies, Integer> getTowServiceProviderCompaniesDao() {
if (null == towServiceProviderCompaniesDao) {
try {
towServiceProviderCompaniesDao = getDao(ServiceProviderCompanies.class);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return towServiceProviderCompaniesDao;
}
i called it in my activity in like this:
String[] ServicePCompanies;
try {List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll();
ServicePCompanies=new String[mSerProCom.size()];
for(int i=1;i<mSerProCom.size();i++){
ServicePCompanies[i]=mSerProCom.get(i)._serviceRequesterServiceProviderCompany;
}
//CompaniesForSpinner=towManager.getTowServiceProviderCompaniesDao().queryForAll().toString();
} catch (SQLException e) {
e.printStackTrace();
}
and i filled my spinner using these lines of code
Spinner entrAssistanceList=(Spinner)findViewById(R.id.SpNomEntrAssis);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item,ServicePCompanies);
spinnerArrayAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
entrAssistanceList.setAdapter(spinnerArrayAdapter);
when i do so i get this error:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
at android.widget.Spinner.onMeasure(Spinner.java:454)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:714)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:604)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:714)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:604)
at android.view.View.measure(View.java:15811)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1228)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:326)
at android.widget.ScrollView.onMeasure(ScrollView.java:321)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:714)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:604)
at android.view.View.measure(View.java:15811)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:326)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1427)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:714)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:604)
at android.view.View.measure(View.java:15811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:326)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2209)
at android.view.View.measure(View.java:15811)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2232)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1310)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1505)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4962)
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:5449)
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)
knowing that i tryed to fill the Spinner with an Array that i created manually:
ServicePCompaniesArray=new String[]{"Entreprise1","Entreprise2","Entreprise3"};
and i don't get the error so i think that the problem is when i get the informations from the database, knwoing that i debugged it and the String Array that i'm giving to the adapter is filled with the information that i get from the database, so really i don't know where the NullPointerException comes from
Thank you guys,
you don't have to put your data in this array
String[] ServicePCompanies;
just use the list you get from the database
List<ServiceProviderCompanies> mSerProCom=towManager.getTowServiceProviderCompaniesDao().queryForAll();
don't forget to override toString() method in your object (ServiceProviderCompanies)
At first print your array to Log to make sure sure about your array then do that. If the array contains null then it will give you NullPointerException.So first check the array.Better you can put a if statement to check if the array is empty or not?
Thanks..
EDIT
please replace:
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item,ServicePCompanies);
with this:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ServicePCompanies);
hope it will work..

RejectedExecutionException when loading bitmaps for ListView with asyncTask

I'm trying to create a ListView with bitmaps that are generated on demand. I'm using https://github.com/chrisbanes/Android-BitmapCache to cache the bitmaps
I'm using a SimpleAdapter, with this :
setViewBinder(new SimpleAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Object object,String string) {
if( string.startsWith("ClassName:") ){
final ImageView yourImageView=(ImageView) view;
AsyncTask<String, Void, Bitmap> imageLoadAsyncTask = new AsyncTask<String, Void, Bitmap>() {
String cname;
#Override
protected Bitmap doInBackground(String... classnames) {
cname=classnames[0];
return db.getCharIcon(classnames[0]);
}
#Override
protected void onPostExecute(Bitmap bitmap) {
yourImageView.setImageBitmap(bitmap);
}
};
imageLoadAsyncTask.execute(string.substring(TextUtils.getTrimmedLength("ClassName:")));
return true;
}
return false;
}
}
the getCharIcon function is here :
Bitmap getCharIcon(String classname){
Bitmap modBmp;
CacheableBitmapDrawable cacheBmp=mCache.get(classname);
if(cacheBmp==null)
{
modBmp=createCharIcon(classname);
mCache.put(classname,modBmp);
}
else
{
modBmp=cacheBmp.getBitmap();
}
return modBmp;
}
where createCharIcon generates the bitmap this way (I have yet to implement the final version that selects the good portion of the image):
Bitmap createCharIcon(String classname)
{
Bitmap modBmp = Bitmap.createBitmap(srcBmp,0,0,60,60);
return modBmp;
}
Sadly I am getting this error :
07-02 14:13:04.248 11592-11592/com.lectem.gecharacters E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.util.concurrent.RejectedExecutionException
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1876)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:774)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1295)
at android.os.AsyncTask.execute(AsyncTask.java:394)
at com.lectem.gecharacters.CharFragment$1.setViewValue(CharFragment.java:110)
at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:168)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1730)
at android.widget.ListView.fillDown(ListView.java:655)
at android.widget.ListView.fillSpecific(ListView.java:1287)
at android.widget.ListView.layoutChildren(ListView.java:1573)
at android.widget.AbsListView.onLayout(AbsListView.java:1147)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1241)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1045)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
I get this error when I scroll the list back too fast, so I suppose it is linked with the number of asyncTasks I can use...
I'm really not sure if this is how I should do it or not, but the library bitmapLruCache needs to be ran on something else than the main thread.
Usually, that error means that you are trying to use too many AsyncTasks at once. In an AdapterView, this can occur if you are forking an AsyncTask for every item (e.g., every row in a ListView) without taking into account row recycling, and the user flings the list.
Quoting the library's documentation:
If you wish for the library and recycling feature to work, you MUST use the bundled CacheableImageView wherever possible.
Since you appear to be using this in an AdapterView, please make sure that you are using CacheableImageView.

Calling getActivity() inside a Fragment returns null

excuse the trouble I was trying to make a listview for parsing a page for my app, I'll explain: I have a actionbar formed by Fragment.
When instazia the fragment, opening the app the first time "getActivity" does not return null, is the next time, when I start the AsyncTask
In the fragment in which I will stop parsing this error:
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
at com.megadown.megacodownloader.ParsingArrayAdapter.<init>(ParsingArrayAdapter.java:30)
at com.megadown.megacodownloader.Tab_Search$ParsingPaginaWeb.onPostExecute(Tab_Search.java:264)
at com.megadown.megacodownloader.Tab_Search$ParsingPaginaWeb.onPostExecute(Tab_Search.java:125)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Code Java that refers:
protected void onPostExecute(String result)
{
// dopo che ho eseguito il parsing mostro i dati nella listview
adapter = new ParsingArrayAdapter(myActivity, titoli, descrizioni,immagini);
lista.setAdapter(adapter);
}
If you want I can post the code of the ArrayAdapter.
Thank you in advance
First, make sure myActivity has the correct value:
protected void onPostExecute(String result) {
Activity myActivity = getActivity();
adapter = new ParsingArrayAdapter(myActivity, titoli, descrizioni,immagini);
lista.setAdapter(adapter);
}
Next, point is, you can't be sure that your activity and fragment is still active in onPostExecute().
For example, the user could have pressed 'back' button or rotated the screen, which would re-create the activites and fragments.
So, the good code is:
protected void onPostExecute(String result) {
Activity myActivity = getActivity();
if (myActivity==null) return; // Fragment not active anymore, bail out
adapter = new ParsingArrayAdapter(myActivity, titoli, descrizioni,immagini);
lista.setAdapter(adapter);
}
Fragment's getActivity() will only return expectable value while the associated activity has done onCreate method.

Categories