Stack Exchange
Cannot generate google charts qrcode and save it as a bitmap
I have this code.. the function to get QRcode is not mine .. i found it somewhere on the internet.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_task_detail,
            container, false);


        TextView details = (TextView) rootView.findViewById(R.id.task_detail);
        details.setText(mCompletedTask.awardDescription);

        Bitmap bmp = generateQrcode();
        //details.setBackground(new BitmapDrawable(getResources(),bmp));



    return rootView;
}
private Bitmap generateQrcode() {
    URL aURL;


    try {
        aURL = new URL("http://chart.apis.google.com//chart?chs=150x150&cht=qr&chl=Hello%20afdpf");
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();


        return bm;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

TextView XML...
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/task_detail"
android:contentDescription="@string/taskdetail"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textIsSelectable="true"
tools:context=".taskDetailFragment" />

I get this error from eclipse LogCat .. and my application stops responding
 09-06 22:48:44.342: W/dalvikvm(4258): threadid=1: thread exiting with uncaught exception (group=0x4106a2a0)
09-06 22:48:44.392: E/AndroidRuntime(4258): FATAL EXCEPTION: main
09-06 22:48:44.392: E/AndroidRuntime(4258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.city2/com.example.city2.taskDetailActivity}: android.os.NetworkOnMainThreadException
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.os.Looper.loop(Looper.java:137)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread.main(ActivityThread.java:4867)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at java.lang.reflect.Method.invoke(Method.java:511)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at dalvik.system.NativeStart.main(Native Method)
09-06 22:48:44.392: E/AndroidRuntime(4258): Caused by: android.os.NetworkOnMainThreadException
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at com.example.city2.taskDetailFragment.generateQrcode(taskDetailFragment.java:89)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at com.example.city2.taskDetailFragment.onCreateView(taskDetailFragment.java:73)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.Activity.performStart(Activity.java:5057)
09-06 22:48:44.392: E/AndroidRuntime(4258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
09-06 22:48:44.392: E/AndroidRuntime(4258):     ... 11 more
09-06 22:48:54.472: I/Process(4258): Sending signal. PID: 4258 SIG: 9

Any Ideas ?? i suppose it has something to do with the HTTP connector

You got a android.os.NetworkOnMainThreadException and it's because you are trying to perform network operations from UI thread. Starting with Android 3.0 and higher any attempts to perform network operations from UI thread will throw that exception.  
The solution is to run generateQrCode() method in a separate thread, for example in an AsyncTask, or IntentService.

Thanks for the quick response.. I used AsyncTask, its working..It's been many hours that i am coding and didnt think of that :D
