Problems with executing a method with asynctask from the main activity - android

So, I have an app which uses the Viewpager and swiping. I got 3 fragments which contains a listview, and a custom adapter to set the rows.
In each fragment, I have a method to call the asynctask and fetch the JSON. I run this method in the onActivityCreated.
When I start the app, everything is working fine, the progressdialog shows, and all the list's are being set with the correct data.
The problem is that i want to be able to refresh all the lists when click the refresh-button in the actionbar (to check for new tweets).
The only way I've been able to use the refreshbutton is when I use the onOptionsItemSelected() in the MainActivity.
So I'm thinking, I can just call the methods who then again calls the asynctask.
case R.id.menu_refresh:
MSFTHost host = new MSFTHost();
host.getTweets();
...
But when I click the button, the whole thing force closes:
02-05 21:46:57.959: W/dalvikvm(15292): threadid=1: thread exiting with uncaught exception (group=0x40d5a930)02-05 21:46:58.045:
E/AndroidRuntime(15292): FATAL EXCEPTION: main
02-05 21:46:58.045: E/AndroidRuntime(15292): java.lang.NullPointerException
02-05 21:46:58.045: E/AndroidRuntime(15292): at com.sandan.HostingNorge.MSFTHost$DownloadWebPageTask.onPostExecute(MSFTHost.java:95)
02-05 21:46:58.045: E/AndroidRuntime(15292): at com.sandan.HostingNorge.MSFTHost$DownloadWebPageTask.onPostExecute(MSFTHost.java:1)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.os.Looper.loop(Looper.java:137)
02-05 21:46:58.045: E/AndroidRuntime(15292): at android.app.ActivityThread.main(ActivityThread.java:5202)
02-05 21:46:58.045: E/AndroidRuntime(15292): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 21:46:58.045: E/AndroidRuntime(15292): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 21:46:58.045: E/AndroidRuntime(15292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
02-05 21:46:58.045: E/AndroidRuntime(15292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-05 21:46:58.045: E/AndroidRuntime(15292): at dalvik.system.NativeStart.main(Native Method)
the line 95 contains this:
listItems.add(new GetListItems(oneObject.getString("text"),oneObject.getString("created_at"),obj
.getString("profile_image_url"), obj
.getString("screen_name")));
I tried clearing listItems, but it didn't have any effect.
Any help please?
Edit:
If I was unclear, I want to get the JSON again and show in the list, just like on the first run.

Related

IllegalStateException in Android listview

I used https://github.com/johannilsson/android-pulltorefresh for pull to refresh in my listview
this is the code..........
in AsyncTask onPostExecute I wrote this one
adapter=new ServicesTypeTranDetailAdapter(getActivity(), items_list_trandetails);
servicesList.onRefreshComplete();
servicesList.setAdapter(adapter);
I was doing same functionality I didn't get any error. If am I kept the device idle for two hours then I got this error. Please help me. Thanks in advance...
FATAL EXCEPTION: main
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131165659, class com.isp.activities.PullToRefreshListView) with Adapter(class android.widget.HeaderViewListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1538)
at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:2977)
at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:606)
at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:2761)
at android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:2745)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2872)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Android voice recording crashes (straight from Docs)

I've taken the code from Android's own Audio Capture documentation and ran it to achieve just what the docs say. As is, the code crashes when I click the "Start Recording" button and the crash says the following:
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
**at com.om.shout_o_meter.MainActivity.startRecording(MainActivity.java:130)
at com.om.shout_o_meter.MainActivity.onRecord(MainActivity.java:111)
at com.om.shout_o_meter.MainActivity.access$0(MainActivity.java:109)
at com.om.shout_o_meter.MainActivity$1.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)
If I place AudioRecordTest() at the very beginning of the startRecording() method, the crash ceases until I finish the first recording and then press "Start Recording" again, the crash reappears with the following message:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.om.shout_o_meter.MainActivity.stopRecording(MainActivity.java:135)
at com.om.shout_o_meter.MainActivity.onRecord(MainActivity.java:113)
at com.om.shout_o_meter.MainActivity.access$0(MainActivity.java:109)
at com.om.shout_o_meter.MainActivity$1.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)
The code I'm using is the exact same code as that of the docs (linked above), and the "MainActivity" file that the exception is pointing to can be found here: http://pastie.org/8682455
Any clue? what baffles me is that the code is pasted as is, straight from the docs, and still it fails..
Thanks

FC when looking for record in SQLite

I have several methods for getting a record in my app
But for some reason this one keeps giving me FC !
I'm using the following method to get the Cursor :
Cursor c=helper.getRecordById(num);
Which is implemented as follows :
public Cursor getRecordById(int rowId) {
String rowIDs=""+rowId;
String SelectById = "SELECT id, OrderName, OrderLink, DateYear, DateMonth, DateDay, OrderPrice FROM Orders WHERE id=?";
String[] args={rowIDs};
return(getReadableDatabase().rawQuery(SelectById, args));
}
I have tried also without converting the Int to String but i just left it this way.. get the same error for both ways.
Whenever i get to the following line :
String name=c.getString(c.getColumnIndexOrThrow("OrderName"));
I get the FC.
The GetColumnIndex works just fine... i've tested it exclusively.
But the getString throws the error. :
06-02 21:07:13.656: W/KeyCharacterMap(4329): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-02 21:07:16.006: D/AndroidRuntime(4329): Shutting down VM
06-02 21:07:16.006: W/dalvikvm(4329): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-02 21:07:16.026: E/AndroidRuntime(4329): FATAL EXCEPTION: main
06-02 21:07:16.026: E/AndroidRuntime(4329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Sagi.MyOrders/com.Sagi.MyOrders.DisplayRecord}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.os.Looper.loop(Looper.java:123)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 21:07:16.026: E/AndroidRuntime(4329): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 21:07:16.026: E/AndroidRuntime(4329): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 21:07:16.026: E/AndroidRuntime(4329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 21:07:16.026: E/AndroidRuntime(4329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 21:07:16.026: E/AndroidRuntime(4329): at dalvik.system.NativeStart.main(Native Method)
06-02 21:07:16.026: E/AndroidRuntime(4329): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
06-02 21:07:16.026: E/AndroidRuntime(4329): at com.Sagi.MyOrders.DisplayRecord.onCreate(DisplayRecord.java:48)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 21:07:16.026: E/AndroidRuntime(4329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 21:07:16.026: E/AndroidRuntime(4329): ... 11 more
06-02 21:12:16.126: I/Process(4329): Sending signal. PID: 4329 SIG: 9
The funny thing is i copied the same exact Lookup method i used in another place which works just fine !, i even tried giving it a constant to look for which i know to exists just to check. but nothing. error over and over again !
Thanks !!!
A Cursor initially points to a record one before the start, to allow this kind of code:
final Cursor cursor = /* get cursor */;
while(cursor.moveToNext()) {
/* do something with cursor */
}
If you are only expecting one record, and you are sure there will be one record, then before accesing your Cursor do:
cursor.moveToNext();
or:
cursor.moveToFirst();

runTime error in libGDX

in past i worked with my projects and everything was good. but recently, i have this errors:
06-02 04:52:27.954: W/dalvikvm(344): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/badlogic/gdx/backends/android/AndroidApplication;
06-02 04:52:27.954: W/dalvikvm(344): Class init failed in newInstance call (Lcom/Jumper/ProjectAndroidActivity;)
06-02 04:52:27.963: D/AndroidRuntime(344): Shutting down VM
06-02 04:52:27.963: W/dalvikvm(344): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-02 04:52:27.993: E/AndroidRuntime(344): FATAL EXCEPTION: main
06-02 04:52:27.993: E/AndroidRuntime(344): java.lang.ExceptionInInitializerError
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.Class.newInstanceImpl(Native Method)
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.Class.newInstance(Class.java:1409)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.os.Looper.loop(Looper.java:123)
06-02 04:52:27.993: E/AndroidRuntime(344): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.reflect.Method.invoke(Method.java:507)
06-02 04:52:27.993: E/AndroidRuntime(344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-02 04:52:27.993: E/AndroidRuntime(344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-02 04:52:27.993: E/AndroidRuntime(344): at dalvik.system.NativeStart.main(Native Method)
06-02 04:52:27.993: E/AndroidRuntime(344): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gdx: findLibrary returned null
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.Runtime.loadLibrary(Runtime.java:429)
06-02 04:52:27.993: E/AndroidRuntime(344): at java.lang.System.loadLibrary(System.java:554)
06-02 04:52:27.993: E/AndroidRuntime(344): at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:116)
06-02 04:52:27.993: E/AndroidRuntime(344): at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:59)
06-02 04:52:27.993: E/AndroidRuntime(344): ... 15 more
i think this errors are from my libs. but i change them with new ones. is there any solution for it?
This error occurs when the armeabi and armeabi-v7a directories cannot be found. Copy both of these directories and their contents into the libs directory of your project.
It should look something like this:
you copied the wrong .so files in the folders. I assume you extracted them from gdx-natives.jar. Instead directly copy the armeabi and armeabi-v7a folders from the release/nightly. Make sure to use jars and natives only from a single release, do not mix jars and natives from multiple releases.
Alternatively use the gdx-setup-ui, as described here http://code.google.com/p/libgdx/wiki/ProjectSetupNew
I got that same error on my VM and other emulators, and found it was because they ran x86 O/S'es, which libgdx does not support.
If it doesn't work on the device either, try this:
android-project -> properties -> java Build Path -> Order and Export.
Click the project you've linked to (your plain java-project or desktop-project) and click the "Up"-button until it is at the very top. Make sure it's ticked [V].
I did that with the jars too, just in case, and now it runs on my device. I still get that same error on my x86 VM, because libgdx does not support x86 Android devices.

Android Set textview layout width dynamically

I have a listview that consists of two textviews. One for dates, one for names.
Config.xml includes the listview:
<ListView android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linlay">
</ListView>
Row.xml includes the two textviews:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout android:orientation="horizontal"
android:id="#+id/linlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</LinearLayout>
</LinearLayout>
I have two more buttons to change the content of the textviews with each other but as the content of the first one (initially) is wider than the second one, i need to set the width of the layout in the code.
Unfortunately there is no txt.setLayoutWidth or similar command, and txt.setWidth() is not working.
I need something like this in this code:
Button sortname = (Button)findViewById(R.id.Button02);
sortname.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
lv1.setAdapter(simpleAdapter2);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(200,120);
txt1.setLayoutParams(layoutparams);
}
});
This two rows makes the app freeze (stopped unexpectedly):
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(200,120);
txt1.setLayoutParams(layoutparams);
Logcat:
02-05 22:58:11.040: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.bfarago.nevnap/.MainActivity }
02-05 22:58:11.210: DEBUG/AndroidRuntime(415): Shutting down VM
02-05 22:58:11.230: DEBUG/dalvikvm(415): Debugger has detached; object registry had 1 entries
02-05 22:58:11.330: INFO/AndroidRuntime(415): NOTE: attach of thread 'Binder Thread #3' failed
02-05 22:58:12.310: INFO/ActivityManager(58): Displayed activity com.bfarago.nevnap/.MainActivity: 1108 ms (total 1108 ms)
02-05 22:58:17.560: DEBUG/dalvikvm(125): GC_EXPLICIT freed 1274 objects / 73520 bytes in 185ms
02-05 22:58:17.660: DEBUG/AndroidRuntime(405): Shutting down VM
02-05 22:58:17.660: WARN/dalvikvm(405): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
02-05 22:58:17.670: ERROR/AndroidRuntime(405): java.lang.NullPointerException
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.bfarago.nevnap.MainActivity$2.onClick(MainActivity.java:129)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.view.View.performClick(View.java:2408)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.view.View$PerformClick.run(View.java:8816)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Handler.handleCallback(Handler.java:587)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.os.Looper.loop(Looper.java:123)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at java.lang.reflect.Method.invoke(Method.java:521)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-05 22:58:17.670: ERROR/AndroidRuntime(405): at dalvik.system.NativeStart.main(Native Method)
02-05 22:58:17.680: WARN/ActivityManager(58): Force finishing activity com.bfarago.nevnap/.MainActivity
02-05 22:58:18.200: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43e9a940 com.bfarago.nevnap/.MainActivity}
02-05 22:58:19.790: INFO/Process(405): Sending signal. PID: 405 SIG: 9
02-05 22:58:19.820: INFO/ActivityManager(58): Process com.bfarago.nevnap (pid 405) has died.
02-05 22:58:19.820: WARN/ActivityManager(58): Scheduling restart of crashed service com.bfarago.nevnap/.UpdateService in 5000ms
02-05 22:58:19.820: INFO/WindowManager(58): WIN DEATH: Window{43fce1d8 com.bfarago.nevnap/com.bfarago.nevnap.MainActivity paused=false}
02-05 22:58:19.920: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 405 uid 10048
02-05 22:58:24.291: DEBUG/dalvikvm(216): GC_EXPLICIT freed 93 objects / 3736 bytes in 153ms
02-05 22:58:24.901: INFO/ActivityManager(58): Start proc com.bfarago.nevnap for service com.bfarago.nevnap/.UpdateService: pid=425 uid=10048 gids={1015}
02-05 22:58:28.966: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43e9a940 com.bfarago.nevnap/.MainActivity}
02-05 22:58:29.350: DEBUG/dalvikvm(263): GC_EXPLICIT freed 44 objects / 2032 bytes in 164ms
You want to use the method setLayoutParams of TextView and pass into it a new LinearLayout.LayoutParams(width, height) where width and height are integers that will specify the width and height. you can keep the height and set the width to whatever you want.

Categories

Resources