[Java for Android App Development] hiphop12ism:
Hello guys,

Well, I was working with data sharing via http. So, I made an app in which the app downloads a image after a certain button is clicked. However the app is crashing at that moment.

But when I execute

new DownloadImageTask().execute(myUrl);

from OnCreate, the app just works fine. I am completely lost here. Please help me out!

I am giving you the MainActivity.class and the logcat.

Thanks

MainActivity.class

package com.example.neew;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener{
ImageView img;
TextView text;
Thread t;
Button b;
Handler handler=new Handler();
String myUrl="http://s27.postimg.org/epsajh82q/Nissan_Skyline_Wallpaper_Photos_HD.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.images);
text=(TextView)findViewById(R.id.tex);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);

}

@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);
}
private InputStream openHttpConnection(String urlString) throws IOException{
InputStream in=null;
int response=-1;
URL url=new URL(urlString);
URLConnection urlconnection=url.openConnection();
if(!(urlconnection instanceof HttpURLConnection )){
Toast.makeText(getBaseContext(),
"Not an Http protocol",
Toast.LENGTH_LONG).show();
}else{
try{
HttpURLConnection conn=(HttpURLConnection)urlconnection;
conn.setAllowUserInteraction(false);
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("GET");
conn.connect();
text.setText("conn made");
response=conn.getResponseCode();
if(response==HttpURLConnection.HTTP_OK){
//text.setText("Connection made-Protocol accepted");
runner("Protocol is setuped");
in=conn.getInputStream();
}
}catch(Exception e){
Toast.makeText(getBaseContext(), e.toString(), 3000).show();
}
}
//text.setText("Stream returned");
runner("stream is returned");
return in;
}
private Bitmap downloadImage(String url){
Bitmap bitmap=null;
InputStream input=null;
try{
input=openHttpConnection(url);
bitmap=BitmapFactory.decodeStream(input);

input.close();
}catch(IOException e){
Toast.makeText(getApplicationContext(), e.toString(), 3467).show();
}
runner("Bitmap is downloaded");
return bitmap;
}

public void Theader(final String msg){
t=new Thread(new Runnable(){
public void run(){
handler.post(new Runnable(){
public void run(){
text.setText(msg);
}
});

}
});
t.start();
}

@SuppressWarnings("deprecation")
public void runner(String s){

Theader(s);
if(text.getText().toString()==s)
{
t.stop();
}
}

private class DownloadImageTask extends AsyncTask<String,Void,Bitmap>{

@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
runner("Downloading bitmap");
return downloadImage(params[0]);
}

protected void onPostExecute(Bitmap result){

runner("Bitmap is set");

img.setImageBitmap(result);

}

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DownloadImageTask().execute(myUrl);

}

}

________________________________________________________________________________________________

Logcat

08-16 09:12:22.442: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:22.518: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:22.962: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:23.022: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:23.481: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:23.511: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:23.631: D/gralloc_goldfish(707): Emulator without GPU emulation detected.
08-16 09:25:43.031: W/dalvikvm(707): threadid=11: thread exiting with uncaught exception (group=0x2ba041f8)
08-16 09:25:43.061: E/AndroidRuntime(707): FATAL EXCEPTION: AsyncTask #1
08-16 09:25:43.061: E/AndroidRuntime(707): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.lang.Thread.run(Thread.java:856)
08-16 09:25:43.061: E/AndroidRuntime(707): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.os.Handler.<init>(Handler.java:121)
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.widget.Toast$TN.<init>(Toast.java:317)
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.widget.Toast.<init>(Toast.java:91)
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.widget.Toast.makeText(Toast.java:233)
08-16 09:25:43.061: E/AndroidRuntime(707):	at com.example.neew.MainActivity.openHttpConnection(MainActivity.java:86)
08-16 09:25:43.061: E/AndroidRuntime(707):	at com.example.neew.MainActivity.downloadImage(MainActivity.java:97)
08-16 09:25:43.061: E/AndroidRuntime(707):	at com.example.neew.MainActivity.access$0(MainActivity.java:93)
08-16 09:25:43.061: E/AndroidRuntime(707):	at com.example.neew.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:138)
08-16 09:25:43.061: E/AndroidRuntime(707):	at com.example.neew.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:1)
08-16 09:25:43.061: E/AndroidRuntime(707):	at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-16 09:25:43.061: E/AndroidRuntime(707):	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 09:25:43.061: E/AndroidRuntime(707):	... 5 more
08-16 09:25:47.902: I/Process(707): Sending signal. PID: 707 SIG: 9