Broadcastreceiver App Crash

时间:2017-10-23 10:26:28

标签: java android broadcastreceiver

我是Android编程的新手,我正在尝试学习如何使用广播接收器在背景中下载图像。现在,每次运行应用程序时,应用程序都会崩溃。我一直在努力,但徒劳无功。我希望你们能帮助我。

这是代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button go, kill;

public static ProgressBar progressBar1, progressBar2, progressBar3, progressBar4, progressBar5;

public static ImageView imageView1, imageView2, imageView3, imageView4, imageView5;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    go = (Button) findViewById(R.id.goButton);
    kill = (Button) findViewById(R.id.killButton);

    progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
    progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
    progressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
    progressBar4 = (ProgressBar) findViewById(R.id.progressBar4);
    progressBar5 = (ProgressBar) findViewById(R.id.progressBar5);

    imageView1 = (ImageView) findViewById(R.id.imageView1);
    imageView2 = (ImageView) findViewById(R.id.imageView2);
    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView4 = (ImageView) findViewById(R.id.imageView4);
    imageView5 = (ImageView) findViewById(R.id.imageView5);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(getString(R.string.downloaded));
    registerReceiver(downloadReceiver, intentFilter);

    go.setOnClickListener(this);
    kill.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    switch(v.getId()){

        case R.id.goButton:

            Intent intent = new Intent(MainActivity.this, Interaction.class);
            startService(intent);
            break;

        case R.id.killButton:

            finish();
            break;

    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(downloadReceiver);
}

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        progressBar1.setVisibility(View.GONE);

    }
};


}

相互作用

public class Interaction extends Service {

final class MyThreadClass implements Runnable{

    int service_id;
    MyThreadClass(int service_id){

        this.service_id = service_id;

    }

    @Override
    public void run() {

        // new
        MainActivity.imageView1.setImageBitmap(getBitmapFromURL(getString(R.string.link1)));
        MainActivity.imageView1.setVisibility(View.VISIBLE);
        Intent intent1 = new Intent(getString(R.string.downloaded));
        sendBroadcast(intent1);

        stopSelf(service_id);

    }
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Toast.makeText(this,"Downloading Photos", Toast.LENGTH_LONG).show();
    Thread thread = new Thread(new MyThreadClass(startId));
    thread.start();
    return START_STICKY;

}

@Override
public void onDestroy() {
    super.onDestroy();
}

private Bitmap getBitmapFromURL(String src){

    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;

    }catch (Exception e){

        e.printStackTrace();
        return null;

    }

}

}

这是logcat:

---------崩溃的开始

10-23 10:50:15.322 2568-3873/ziad.example.com.downloadapp E/AndroidRuntime: FATAL EXCEPTION: Thread-4
                                                                            Process: ziad.example.com.downloadapp, PID: 2568
                                                                            android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                                at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6891)
                                                                                at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1048)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
                                                                                at android.view.View.requestLayout(View.java:19781)
                                                                                at android.widget.ImageView.setImageDrawable(ImageView.java:529)
                                                                                at android.widget.ImageView.setImageBitmap(ImageView.java:659)
                                                                                at ziad.example.com.downloadapp.Interaction$MyThreadClass.run(Interaction.java:39)
                                                                                at java.lang.Thread.run(Thread.java:761)

0 个答案:

没有答案