My application does not give any compile-time error and it does show the activity like it show. It is a simple activity with a button. When the user clicks on it, it should download an image from the network. When I click on the button, it just says, "Unfortunately, Concurrency has stopped."
I am reading up on threads in android and I got to know about the rules of not performing intensive operations in the UI thread and not manipulating the UI from outside the UI thread. I was practicing this.
I am not yet familiar with how to read logcats. I am posting my logcat and it will be great if somebody can read it and point out what's causing the problem.
Moreover, the implementation of loadImageFromNetwork() is not coded by me. I just copied it from internet. So I don't have an understanding of its call to the decodeStream() method. It was not my concern for the moment.
Main.java:-
package com.example.concurrency;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void downloadImage(){
new Thread(new Runnable(){
private Bitmap loadImageFromNetwork(String url){
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void run(){
final Bitmap bitmap= loadImageFromNetwork("http://www.google.com/imgres?imgurl=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F7%2F7a%2FBasketball.png&imgrefurl=http%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ABasketball.png&h=340&w=340&tbnid=EJmjEDyJzrhAuM%3A&zoom=1&docid=C_hn8nOgsGmuwM&hl=en&ei=Q0o2U93LNcaIygH4mICQBQ&tbm=isch&ved=0CHwQhBwwBg&iact=rc&dur=3875&page=1&start=0&ndsp=14");
imageView.post(new Runnable(){
public void run(){
imageView.setImageBitmap(bitmap);
}
});
}
}).start();
}
}
Activity_main.xml:-
<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"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_MainActivity"
android:onClick="downloadImage"/>
</RelativeLayout>
String.xml:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Concurrency</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button_MainActivity">Download</string>
</resources>
Manifest.xml:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.concurrency"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.concurrency.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>
Logcat:-
It exceeded the length of the question. So you can find it here.