Android : can't load my XML file and app crashes - android

So I just created a parser and a handler to parse an XML file. It works fine in my Java test project but doesn't in my Android project. Here is the complaining log :
07-12 09:44:59.586: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ec.ec/com.ec.ec.ListeOffresActivity}: android.os.NetworkOnMainThreadException
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:137)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 09:44:59.586: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-12 09:44:59.586: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-12 09:44:59.586: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
07-12 09:44:59.586: E/AndroidRuntime(1164): Caused by: android.os.NetworkOnMainThreadException
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.(HttpConnection.java:70)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.URL.openStream(URL.java:462)
07-12 09:44:59.586: E/AndroidRuntime(1164): at com.ec.ec.ListeOffresActivity.onCreate(ListeOffresActivity.java:31)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.Activity.performCreate(Activity.java:5008)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-12 09:44:59.586: E/AndroidRuntime(1164): ... 11 more
It appears that my emulator isn't connected to the Internet, right ? What should I do ?
Thanks for your help

You are running your Network request on main UI thread. use AsyncTask to execute network request.

The log is not very cleay. But I can see the below error -
07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.URL.openStream(URL.java:462)
So most likely the URL is not accesisble from emulator, as you are able to access and parse the XMl from your java code.
Can you launch the browser from your emulator and see if the URL or even the internet is accessible?
-dattatray.

Related

add google maps in an android application

I'm new in Android development. I want to add Google maps to my application. I have done lots of searches but still the same problem arises when I run the application "Unfortunately , application has stopped"
So this is my java class
package com.example.hellomap;
import com.google.android.maps.MapView;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
MapView map =(MapView) findViewById(R.id.map);
}
}
the xml file :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
and the Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellomap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.example.hellomap.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCKSp9pb7g8Wkga2SgeoT_MSe0JG59DkSU" />
</application>
</manifest>
The LogCat shows:
04-03 13:45:42.574: E/AndroidRuntime(1164): FATAL EXCEPTION: main
04-03 13:45:42.574: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:137)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 13:45:42.574: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 13:45:42.574: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 13:45:42.574: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 13:45:42.574: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 13:45:42.574: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
04-03 13:45:42.574: E/AndroidRuntime(1164): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-03 13:45:42.574: E/AndroidRuntime(1164): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Activity.setContentView(Activity.java:1867)
04-03 13:45:42.574: E/AndroidRuntime(1164): at com.example.hellomap.MainActivity.onCreate(MainActivity.java:23)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Activity.performCreate(Activity.java:5008)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-03 13:45:42.574: E/AndroidRuntime(1164): ... 11 more
04-03 13:45:42.574: E/AndroidRuntime(1164): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Fragment.instantiate(Fragment.java:584)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Fragment.instantiate(Fragment.java:552)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Activity.onCreateView(Activity.java:4656)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-03 13:45:42.574: E/AndroidRuntime(1164): ... 20 more
04-03 13:45:42.574: E/AndroidRuntime(1164): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
04-03 13:45:42.574: E/AndroidRuntime(1164): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-03 13:45:42.574: E/AndroidRuntime(1164): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-03 13:45:42.574: E/AndroidRuntime(1164): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-03 13:45:42.574: E/AndroidRuntime(1164): at android.app.Fragment.instantiate(Fragment.java:574)
04-03 13:45:42.574: E/AndroidRuntime(1164): ... 23 more
04-03 13:50:42.626: I/Process(1164): Sending signal. PID: 1164 SIG: 9
Thanks for help :)
EXCEPTION :- ClassNotFoundException
please check GooglePlayService Library Project First.
Second Thing You Can Not Type Cast Fragment To Map View, please read this
Update:-
Follow The Step:-
1)Create New Project
2)Import GooglePlayService Library project into Your Workspace.
3)assign Google play Service library project as library project to your project
4)now add Map Fragment/Supported Map Fragment into your activity layout file
5)make sure this class found
how to check==>press control Key from keyboard and click on class name id you found class then it's okay other wise fix it first.
i prefer to use Supported Fragment.
6)define all permission as describe in link i posted previously.
6)now its time to generate api key for map ,follow the step from given link.
Thanks i hope its help

Memory Issue in my android application

07-12 12:01:47.016: E/AndroidRuntime(24203): FATAL EXCEPTION: main
07-12 12:01:47.016: E/AndroidRuntime(24203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oman.muscat/com.oman.muscat.settings.Settings}: android.view.InflateException: Binary XML file line #55: Error inflating class <unknown>
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.os.Looper.loop(Looper.java:137)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-12 12:01:47.016: E/AndroidRuntime(24203): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 12:01:47.016: E/AndroidRuntime(24203): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-12 12:01:47.016: E/AndroidRuntime(24203): at dalvik.system.NativeStart.main(Native Method)
07-12 12:01:47.016: E/AndroidRuntime(24203): Caused by: android.view.InflateException: Binary XML file line #55: Error inflating class <unknown>
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.muscat.menudrawer.MenuDrawer.setContentView(MenuDrawer.java:1055)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.oman.muscat.settings.Settings.onCreate(Settings.java:92)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.Activity.performCreate(Activity.java:5206)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-12 12:01:47.016: E/AndroidRuntime(24203): ... 11 more
07-12 12:01:47.016: E/AndroidRuntime(24203): Caused by: java.lang.reflect.InvocationTargetException
07-12 12:01:47.016: E/AndroidRuntime(24203): at java.lang.reflect.Constructor.constructNative(Native Method)
07-12 12:01:47.016: E/AndroidRuntime(24203): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-12 12:01:47.016: E/AndroidRuntime(24203): ... 23 more
07-12 12:01:47.016: E/AndroidRuntime(24203): Caused by: java.lang.OutOfMemoryError
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.content.res.Resources.loadDrawable(Resources.java:1963)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.View.<init>(View.java:3436)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.ViewGroup.<init>(ViewGroup.java:432)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.widget.LinearLayout.<init>(LinearLayout.java:176)
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.widget.LinearLayout.<init>(LinearLayout.java:172)
07-12 12:01:47.016: E/AndroidRuntime(24203): ... 26 more
Please give some solution to fix this issue.
You seem to be loading large bitmaps in your activity and the device is running out of memory. Here's a guide to displaying bitmaps properly in Android - http://developer.android.com/training/displaying-bitmaps/index.html
From the logs, it seems like the FATAL exception is caused by java.lang.OutOfMemoryError.
which means that the bitmaps which are created during aren't deallocated properly.
07-12 12:01:47.016: E/AndroidRuntime(24203): Caused by: android.view.InflateException: Binary XML file line #55: Error inflating class <unknown>
07-12 12:01:47.016: E/AndroidRuntime(24203): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-12 12:01:47.016: E/AndroidRuntime(24203): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
have you checked whether you are deallocating bitmaps, while pausing or destroying the activity or Not.(in onDestroy() or in onPause()).

Why is executing this HttpGet in Android raising a NullPointerException?

I've created a new Android project in which the only code I've added is this code in the onCreate() method (I also added the INTERNET permission to the manifest):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_get_test);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try
{
System.out.println("making uri");
URI uri = new URI("http://w3mentor.com/");
request.setURI(uri);
System.out.println("executing");
client.execute(request); // This line throws the exception
System.out.println("Done!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
In the log, I see that the last thing printed by the try block was executing, after which a NullPointerException was thrown.
I loosely based my code on the beginning of this example, but I can't see what I'm doing differently that would cause a NullPointerException.
When run in the debugger I see that the exception is actually a NetworkOnMainThreadException, which apparently causes a NullPointerException when I try e.getMessage().
The error message being printed out is
07-12 02:58:33.997: E/AndroidRuntime(983): FATAL EXCEPTION: main
07-12 02:58:33.997: E/AndroidRuntime(983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatever.http.test/com.whatever.http.test.HttpGetTest}: java.lang.NullPointerException
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.os.Looper.loop(Looper.java:137)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 02:58:33.997: E/AndroidRuntime(983): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 02:58:33.997: E/AndroidRuntime(983): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 02:58:33.997: E/AndroidRuntime(983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-12 02:58:33.997: E/AndroidRuntime(983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-12 02:58:33.997: E/AndroidRuntime(983): at dalvik.system.NativeStart.main(Native Method)
07-12 02:58:33.997: E/AndroidRuntime(983): Caused by: java.lang.NullPointerException
07-12 02:58:33.997: E/AndroidRuntime(983): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
07-12 02:58:33.997: E/AndroidRuntime(983): at com.whatever.http.test.HttpGetTest.onCreate(HttpGetTest.java:33)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.Activity.performCreate(Activity.java:4465)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-12 02:58:33.997: E/AndroidRuntime(983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-12 02:58:33.997: E/AndroidRuntime(983): ... 11 more
The Issue is not a NullPointerException but if you debug your application, you will find that the exception is : NetworkOnMainThreadException.
Most likely you are running your application on the newer SDKs (Honeycomb or greater) as a result of which this is discouraged.
Any networking code, you should put in a thread. Ideally use the AsyncTask for that. Check this link out : http://www.techblogistech.com/2011/11/how-to-fix-the-android-networkonmainthreadexception/
Hope this helps.

Crash while playing a song with default music player

I want to play some audio files with the default music player. As mentioned in this post, I've tried the following code:
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.music", "com.android.music.MediaPlaybackActivity");
intent.setComponent(comp);
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(songPath.toString());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
But, I'm getting ActivityNotFound Exception with the following logs:
07-12 11:28:49.575: E/AndroidRuntime(22700): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jusplay/com.example.jusplay.playdb}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.music/com.android.music.MediaPlaybackActivity}; have you declared this activity in your AndroidManifest.xml?
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.os.Looper.loop(Looper.java:130)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-12 11:28:49.575: E/AndroidRuntime(22700): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:28:49.575: E/AndroidRuntime(22700): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 11:28:49.575: E/AndroidRuntime(22700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
07-12 11:28:49.575: E/AndroidRuntime(22700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
07-12 11:28:49.575: E/AndroidRuntime(22700): at dalvik.system.NativeStart.main(Native Method)
07-12 11:28:49.575: E/AndroidRuntime(22700): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.music/com.android.music.MediaPlaybackActivity}; have you declared this activity in your AndroidManifest.xml?
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.Activity.startActivityForResult(Activity.java:2827)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.Activity.startActivity(Activity.java:2933)
07-12 11:28:49.575: E/AndroidRuntime(22700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
I'm unable to figure out why this crash is happening !!

Illegal state exception when querying MediaStrore.Images

I am getting a IllegalStateException when I run the following code:
Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null);
imageCursor.moveToFirst();
String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));
Toast.makeText(getApplicationContext(), String.valueOf(imageSize), Toast.LENGTH_LONG).show();
Here is the stacktrace
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.database.CursorWindow.getLong_native(Native Method)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.database.CursorWindow.getLong(CursorWindow.java:380)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.database.CursorWrapper.getLong(CursorWrapper.java:127)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at com.messageHider.viewImageThumb$1.onClick(viewImageThumb.java:93)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.os.Looper.loop(Looper.java:123)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at java.lang.reflect.Method.invoke(Method.java:521)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397): at dalvik.system.NativeStart.main(Native Method)
The error is at:
Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));
Include SIZE in your query projection(the second param in the content resolvers query method).
its MediaStore.MediaColumns.SIZE i believe.

Categories

Resources