Force Close when following ASyncTask Example - java

I'm getting a force close error when attempting to follow this tutorial:
http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2
Any suggestions? I'm really not sure where I went wrong in this.
MY SOURCE:
public class HttpGetAndroidExample {
// The url of the website. This is just an example
private static final String webSiteURL = "http://www.supercars.net/gallery/119513/2841/5.html";
// The path of the folder that you want to save the images to
private static final String folderPath = "<FOLDER PATH>";
public static void main(String[] args) {
try {
// Connect to the website and get the html
Document doc = Jsoup.connect(webSiteURL).get();
// Get all elements with img tag ,
Elements img = doc.getElementsByTag("img");
for (Element el : img) {
// for each element get the srs url
String src = el.absUrl("src");
System.out.println("Image Found!");
System.out.println("src attribute is : " + src);
getImages(src);
}
} catch (IOException ex) {
System.err.println("There was an error");
Logger.getLogger(HttpGetAndroidExample.class.getName()).log(Level.SEVERE,
null, ex);
}
}
private static void getImages(String src) throws IOException {
String folder = null;
// Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
System.out.println(name);
// Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(
folderPath + name));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
}
Logcat:
07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapUtilization:0.25
07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapIdealFree:8388608
07-18 17:22:06.449: D/ActivityThread(11404): setTargetHeapConcurrentStart:2097152
07-18 17:22:06.469: W/dalvikvm(11404): threadid=1: thread exiting with uncaught exception (group=0x41e1f438)
07-18 17:22:06.469: E/AndroidRuntime(11404): FATAL EXCEPTION: main
07-18 17:22:06.469: E/AndroidRuntime(11404): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.httpgetandroidexample/com.example.httpgetandroidexample.HttpGetAndroidExample}: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.access$700(ActivityThread.java:139)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.os.Looper.loop(Looper.java:137)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.main(ActivityThread.java:4918)
07-18 17:22:06.469: E/AndroidRuntime(11404): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:22:06.469: E/AndroidRuntime(11404): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 17:22:06.469: E/AndroidRuntime(11404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-18 17:22:06.469: E/AndroidRuntime(11404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-18 17:22:06.469: E/AndroidRuntime(11404): at dalvik.system.NativeStart.main(Native Method)
07-18 17:22:06.469: E/AndroidRuntime(11404): Caused by: java.lang.ClassCastException: com.example.httpgetandroidexample.HttpGetAndroidExample cannot be cast to android.app.Activity
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
07-18 17:22:06.469: E/AndroidRuntime(11404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2003)
07-18 17:22:06.469: E/AndroidRuntime(11404): ... 11 more
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpgetandroidexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.httpgetandroidexample.HttpGetAndroidExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

There are two issues:
In manifest, you've declared the HttpGetAndroidExample class as an activity. Contradictory to that, the HttpGetAndroidExample class does not extend the Activiy class. So, its not an activity. That's why the exception HttpGetAndroidExample cannot be cast to android.app.Activity. Either extend it to be an Activity and create the required functions or remove it from the manifest if its not an activity.
Also, there is no where on the tutorial link I could see this class.

Related

How to get the final height of a Linearlayout which is populated later with addView()

I have a LinearLayout to which i populated other views using addview(); but when i try to get the height of the Linear layout, it returns 0.
I used ((LinearLayout)findViewById(R.id.linearId)).getHeight();
view i am adding, also has child views. How do i get the height of the Linearlayout including all the children?
EDIT -
actual code i used to adding view to my linear layout -
final ViewTreeObserver viewTreeObserver = earningsContainer.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
Log.e(TAG, "$$$$$height "+earningsContainer.getHeight());
// remember to remove the listener if possible
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
earningsContainer.addView(getRowTaxEarningsTitle());
for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
earningsContainer.addView(getTaxEarningsRow(taxMainObject
.getTaxEarnings().get(i)));
totalEarnings += taxMainObject.getTaxEarnings().get(i)
.getTotalAmount();
}
earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
totalEarnings + ""));
Stacktrace after using ViewTreeObserver -
07-18 13:40:59.936: E/AndroidRuntime(10058): FATAL EXCEPTION: main
07-18 13:40:59.936: E/AndroidRuntime(10058): Process: com.synthesize.paysal, PID: 10058
07-18 13:40:59.936: E/AndroidRuntime(10058): java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.checkIsAlive(ViewTreeObserver.java:720)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.removeOnGlobalLayoutListener(ViewTreeObserver.java:529)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.synthesize.paysal.TaxinfoActivity$1.onGlobalLayout(TaxinfoActivity.java:125)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:815)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1782)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.handleCallback(Handler.java:733)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.os.Looper.loop(Looper.java:136)
07-18 13:40:59.936: E/AndroidRuntime(10058): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 13:40:59.936: E/AndroidRuntime(10058): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 13:40:59.936: E/AndroidRuntime(10058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 13:40:59.936: E/AndroidRuntime(10058): at dalvik.system.NativeStart.main(Native Method)
After adding a child to your LinearLayout it doesn't perform measure and layout immediately, so at that moment its size is still unknown. You need to wait for for a layout phase to know the dimensions.
You can do it by subclassing your LinearLayout and read it's size in onSizeChanged method etc.
OR you can achieve it without subclassing LinearLayout by setting a OnGlobalLayoutListener on ViewTreeObserver:
myLinearLayout = ((LinearLayout)findViewById(R.id.linearId);
and then add an OnGlobalLayoutListener right before adding your views:
myLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// read height with myLinearLayout.getHeight() etc.
// remember to remove the listener if possible
ViewTreeObserver viewTreeObserver = myLinearLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this);
}
}
}
});
mLinearLayout.addView(view);

Unfortunetly The program has stopped

I wrote an android program. This has multiple tab.. I want when I clicked a tab(Tab1), the tab's special view(GalleryDemoActivity.java) was shown.. But the Program has stopped working and does'nt work truly !
The code is:
src/Tab.java:
package com.AdMd.Plant;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Tabs extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.tabs);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
Intent photosIntent = new Intent(this, GalleryDemoActivity.class);
spec1.setContent(photosIntent);
spec1.setIndicator("اطلاعات گیاه");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("گالری تصاویر");
spec2.setContent(R.id.tab2);
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("گیاهان مشابه");
spec3.setContent(R.id.tab3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
}
}
The logcat messages are:
07-18 08:32:37.402: D/gralloc_goldfish(1068): Emulator without GPU emulation detected.
07-18 08:32:40.272: D/AndroidRuntime(1068): Shutting down VM
07-18 08:32:40.272: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-18 08:32:40.344: E/AndroidRuntime(1068): FATAL EXCEPTION: main
07-18 08:32:40.344: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AdMd.Plant/com.AdMd.Plant.Tabs}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.os.Looper.loop(Looper.java:137)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 08:32:40.344: E/AndroidRuntime(1068): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:32:40.344: E/AndroidRuntime(1068): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 08:32:40.344: E/AndroidRuntime(1068): at dalvik.system.NativeStart.main(Native Method)
07-18 08:32:40.344: E/AndroidRuntime(1068): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:680)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.widget.TabHost.addTab(TabHost.java:236)
07-18 08:32:40.344: E/AndroidRuntime(1068): at com.AdMd.Plant.Tabs.onCreate(Tabs.java:32)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.Activity.performCreate(Activity.java:4465)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 08:32:40.344: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 08:32:40.344: E/AndroidRuntime(1068): ... 11 more
As you see in logcat, it said to me to have a activity Group, But when I use it, the program has some warning and it does'nt work again!
The warning are:
1.The method onCreate(Bundle) from the type ActivityGroup is deprecated
2.The type ActivityGroup is deprecated
What should I've done?
Thanks..
you have to provide action for every tab
just try this code
public class Tabs extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.tabs);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setIndicator("اطلاعات گیاه");
spec1.setContent(new Intent(this,GalleryDemoActivity.class));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("گالری تصاویر");
spec2.setContent(new Intent(this,GalleryDemoActivity.class));
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("گیاهان مشابه");
spec3.setContent(new Intent(this,GalleryDemoActivity.class));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
}
}

Getting a classcastexception when trying a custom marker in google maps

I am trying to create a custom marker in google maps.I want to add a custom layout as the marker
So i used the following code inside a webservice:
protected void onPostExecute(Void result) {
View custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
ImageView iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
pinbit=Bitmap.createScaledBitmap(pinbit,75,56,false);
iv_category_logo.setImageBitmap(pinbit);
//pinbit=MainActivity.getCroppedBitmap(pinbit, 10);
marker.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getApplicationContext(), custom_layout)));
// adding marker
googleMap.addMarker(marker);
}
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
But a classcastexception is shown in the following line:
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
The logcat is shown below:
07-18 16:08:07.617: E/AndroidRuntime(22114): FATAL EXCEPTION: main
07-18 16:08:07.617: E/AndroidRuntime(22114): java.lang.ClassCastException: android.app.Application
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity.createDrawableFromView(MainActivity.java:610)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:1)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.os.Looper.loop(Looper.java:130)
07-18 16:08:07.617: E/AndroidRuntime(22114): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-18 16:08:07.617: E/AndroidRuntime(22114): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:08:07.617: E/AndroidRuntime(22114): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-18 16:08:07.617: E/AndroidRuntime(22114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114): at dalvik.system.NativeStart.main(Native Method)
}
please help!!
The problem is in using of getApplicationContext() method. It is strongly recommended to avoid using it when you're working with some screen local stuff. See documentation for this method. It does not return an Activity object. I would suggest you to use inter-process communication (IPC) to pass data between your Service and Activity. One possible way is here. For another ways you can read Developers page.

Retrieving bitmap image from a URL ,getting nullpointerexception randomly in android

Working on a project with google maps in it.I am trying to retrieve bitmap images from a URL and it works fine ,but not all the time
The code i am using to retrieve the url is inside a async class.
The async class:
public class retrieveimage extends AsyncTask<String,Void,Void>
{
Store_data s;
MarkerOptions marker;
Bitmap pinbit;
public retrieveimage(Store_data s) {
this.s=s;
}
#Override
protected Void doInBackground(String... arg0) {
try {
marker = new MarkerOptions().position(new LatLng(Double.parseDouble(s.store_latitude),Double.parseDouble(s.store_longitude))).title(s.store_name);
String url1=arg0[0];
URL url = new URL(url1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap= BitmapFactory.decodeStream(input);
pinbit=bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
#Override
protected void onPostExecute(Void result) {
View custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
ImageView iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
pinbit=Bitmap.createScaledBitmap(pinbit,68,62,false);
iv_category_logo.setImageBitmap(pinbit);
//pinbit=MainActivity.getCroppedBitmap(pinbit, 10);
marker.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(StoreFinal.this, custom_layout)));
// adding marker
googleMap.addMarker(marker);
}
}
I am calling the async class as:
new retrieveimage(sobj).execute(URL_image+sobj.category+".png");
The problem is that the variable pinbit gets NPE randomly.Many times the desired result is seen.But sometimes out of random i get NPE.So i think there is something wrong with the doInBackground method where the web operation takes place.How do i ensure that the code with the web operation doesnt break?
The LOGCAT:
07-18 18:26:20.289: E/AndroidRuntime(27803): FATAL EXCEPTION: main
07-18 18:26:20.289: E/AndroidRuntime(27803): java.lang.NullPointerException
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:344)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.igloo.storelocater.StoreFinal$retrieveimage.onPostExecute(StoreFinal.java:177)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.igloo.storelocater.StoreFinal$retrieveimage.onPostExecute(StoreFinal.java:1)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.os.Looper.loop(Looper.java:130)
07-18 18:26:20.289: E/AndroidRuntime(27803): at android.app.ActivityThread.main(ActivityThread.java:3689)
07-18 18:26:20.289: E/AndroidRuntime(27803): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 18:26:20.289: E/AndroidRuntime(27803): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-18 18:26:20.289: E/AndroidRuntime(27803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 18:26:20.289: E/AndroidRuntime(27803): at dalvik.system.NativeStart.main(Native Method)
Just try integrating AQuery library and use its callback method which will give you bitmap along with URL which it belongs. Hope it helps!

Android Application crahes when passing strings between classes

I'm trying to pass a string between 2 classes in Android . The first class is "MainActivity" and the second one is "abc" .
Here's my MainActivity code :
package com.example.passstrings;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass = "abcabc";
Intent abc = new Intent(this , abc.class);
abc.putExtra("key", pass);
startActivity(abc);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and here's my abc class code :
package com.example.passstrings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
}
Here's my fragment_main.xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.passstrings.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="69dp"
android:layout_marginTop="72dp"
android:layout_toRightOf="#+id/textView2"
android:text="TextView" />
</RelativeLayout>
and here's my LogCat :
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x008b
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x0099
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.675: D/AndroidRuntime(734): Shutting down VM
04-04 05:34:38.675: W/dalvikvm(734): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:34:38.685: E/AndroidRuntime(734): FATAL EXCEPTION: main
04-04 05:34:38.685: E/AndroidRuntime(734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Looper.loop(Looper.java:123)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:34:38.685: E/AndroidRuntime(734): at dalvik.system.NativeStart.main(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivity(Activity.java:2923)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.example.passstrings.MainActivity.onCreate(MainActivity.java:25)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:34:38.685: E/AndroidRuntime(734): ... 11 more
and here's my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
If I remove "startActivity(abc);" the application doesn't crash, also if use try&catch the application doesn't crash but the same time it doesn't perform the action . So what could be the reason ? and thanks in advance
My LogCat after adding the activity to manifest :
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.555: W/dalvikvm(883): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.555: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x008b
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.565: W/dalvikvm(883): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.565: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x0099
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.775: D/AndroidRuntime(883): Shutting down VM
04-04 05:47:16.775: W/dalvikvm(883): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:47:16.786: E/AndroidRuntime(883): FATAL EXCEPTION: main
04-04 05:47:16.786: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.abc}: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:47:16.786: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at com.example.passstrings.abc.onCreate(abc.java:20)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:47:16.786: E/AndroidRuntime(883): ... 11 more
and my Manifest after the update :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.passstrings.abc" />
</application>
</manifest>
You need to add your second Activity to your Androidmanifest.xml file.
<activity android:name="com.example.passstrings.abc" />
have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
it clearly say's that you have not declared your abc activity in manifest file. have you declared this activity in your AndroidManifest.xml?
so just decalre your abc activity in manifest file.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".abc"
</activity>
</application>
add this line
<activity
android:name=".abc"
</activity>
and also change this in your abc.java
setContentView(R.layout.activity_main);
you have reference of activity_main only you have to set abc.XML or whatever your XML filename for abc. So change it with
setContentView(R.layout.abc);
Edit
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
It is simply ActivityNotFoundException which mean you forgot to declare abc activity in your AndroidManifest.xml file.
Just add following line in your manifest.xml in application tag.
<activity android:name="com.example.passstrings.abc" />
For your new NullPointerException here is the solution
You have defined following .xml in your Activity file activity_main while you have declare TextViews in fragment_main.xml, that's why it is giving NullPointerException.
I suggest you to change the xml from following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main.xml);

Categories