按钮没有打开doInBackground(void ... params)

时间:2017-02-15 16:57:30

标签: android android-studio

我试图将图像发送到服务器。发生的事情是按钮不起作用......当我点击它时没有任何反应。

void onCreate

using System;
using Android.App;
using Android.Widget;
using Android.OS;

namespace Ev3BtCom
{
    [Activity(Label = "Ev3BtCom", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        Ev3Messaging _ev3Messaging = new Ev3Messaging();

        bool isParsing = true;
        TextView infosLabel;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView (Resource.Layout.Main);

            ...
            infosLabel = FindViewById<TextView>(Resource.Id.Infos);

            _ev3Messaging = new Ev3Messaging();


            System.Timers.Timer checkForTime = new System.Timers.Timer(500);
            checkForTime.Elapsed += new System.Timers.ElapsedEventHandler(checkForMessage);
            checkForTime.Enabled = true;



            connectBut.Click += async (object sender, EventArgs e) =>
            {
                bool error = false;
                try
                {
                    await _ev3Messaging.Connect(brickNameET.Text);
                }
                catch (Exception ex)
                {
                    Print(ex.Message);
                    error = true;
                }
                if(!error)
                {
                    Print("Connected");
                    isParsing = false;
                }
            };

            ...//Many use of the print function in the same context WITHOUT CRASH
        }

        async void checkForMessage(object source, System.Timers.ElapsedEventArgs e)
        {
            if (!isParsing)
            {
                isParsing = true;
                byte[] datas = new byte[0];
                try
                {
                    datas = await _ev3Messaging.ReceiveText();
                }catch (Exception ex)
                {
                    await _ev3Messaging.SendText("Text", ex.Message);
                    Print(ex.Message);   // MAKES APP CRASH
                }
                if (datas.Length != 0)
                {
                    string printedText = "Received: ";
                    foreach (byte b in datas)
                        printedText += (int)b + ",";
                    Print(printedText);    // MAKES APP CRASH
                    await _ev3Messaging.SendText("Text", printedText);
                }
                Print("Test");   // MAKES APP CRASH
                isParsing = false;
            }
        }

        void Print(string text)
        {
            infosLabel.Text += text + "\n"; // When I remove this line, there is no more crash
        }
    }
}

void onClick

 imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
 bUploadImage = (Button) findViewById(R.id.bUploadImage);
 uploadImageName = (EditText) findViewById(R.id.etUploadName);

 imageToUpload.setOnClickListener(this);

当我按下按钮@Override public void onClick(View v) { switch (v.getId()) { case R.id.imageToUpload: Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); break; case R.id.bUploadImage: //HERE, THE BUTTON THAT I WANT TO WORK Bitmap image = ((BitmapDrawable) imageToUpload.getDrawable()).getBitmap(); new UploadImage(image, uploadImageName.getText().toString()).execute(); } } private class UploadImage extends AsyncTask<Void, Void, Void> { Bitmap image; String name; public UploadImage(Bitmap image, String name){ this.image = image; this.name = name; } @Override protected Void doInBackground(Void... params) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); Toast.makeText(getApplicationContext(), "UPLOADING...", Toast.LENGTH_SHORT).show(); ... 时,doInBackground没有被调用的问题是什么?

1 个答案:

答案 0 :(得分:1)

您必须使用监听器

初始化bUploadImage按钮
bUploadImage.setOnClickListener(this);

因为目前只有case R.id.imageToUpload正在执行