首页 > 解决方案 > 如何在android studio中正确调用函数

问题描述

遵循对话方法的教程。在代码中,对话框由主活动上的按钮调用,并使用其他活动(称为 radio3x3grid)中的 onClickListener 关闭。它没有问题。现在,我想做的是在接收字符串时调用该函数,但我不明白 View 到底是什么以及如何调用它。使用按钮的方法有 showRadioPopup(View v),当尝试从 onArduinoMessage 方法中调用它时,我无法弄清楚在括号之间放置什么参数才能正确调用它。另外,如果有人可以更清楚地解释 View 类,我将不胜感激。

package com.example.multimediainterfacev11;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.app.Dialog;
import android.graphics.Color;
import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import me.aflak.arduino.Arduino;
import me.aflak.arduino.ArduinoListener;

public class MainActivity extends AppCompatActivity implements ArduinoListener {
    Dialog radioPopup;

    private Arduino arduino;
    private TextView displayFreqType;
    private TextView displayChanFreq;
    private TextView displayChanName;
    private TextView displayChanNr;
    private TextView displaySourceRadio;
    private TextView displaySourcecdplayer;
    private TextView displaySourceAux;
    CardView radioCard;
    CardView cdplayerCard;
    CardView auxCard;

    String messageReceived = "";
    int srcCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arduino = new Arduino(this);

        radioPopup=new Dialog(this);

        displayFreqType = findViewById(R.id.freq_type);
        displayChanFreq = findViewById(R.id.chan_freq);
        displayChanName = findViewById(R.id.chan_name);
        displayChanNr = findViewById(R.id.chan_nr);
        displaySourceRadio = findViewById(R.id.source_radio);
        displaySourcecdplayer = findViewById(R.id.source_cdplayer);
        displaySourceAux = findViewById(R.id.source_aux);

        radioCard = findViewById(R.id.radioCard);
        cdplayerCard = findViewById(R.id.cdplayerCard);
        auxCard = findViewById(R.id.auxCard);
    }

    @Override
    protected void onStart(){
        super.onStart();
        arduino.setArduinoListener((ArduinoListener) this);
    }

    @Override
    public void onArduinoAttached(UsbDevice device){
        displayChanName.setText("Arduino attached...");
        arduino.open(device);
    }

    @Override
    public void onArduinoDetached() {
        displayChanName.setText("Arduino dettached...");
    }

    @Override
    public void onArduinoMessage(byte[] bytes) {
        String str = new String(bytes);
        messageReceived =messageReceived + str;

        if (messageReceived.contains("abc")){
            if (messageReceived.toLowerCase().contains("rmd")){
                display(messageReceived);
                messageReceived = "";
            }

            if (messageReceived.toLowerCase().contains("source")){
                String[] source = messageReceived.split(":");
                if (source[1].toLowerCase().contains("radio")) changeColour(1);
                if (source[1].toLowerCase().contains("cdplayer")) changeColour(2);
                if (source[1].toLowerCase().contains("aux")) changeColour(3);
                messageReceived = "";
            }

            if (messageReceived.toLowerCase().contains("popup")){
                //How do i call this method?
                showRadioPopup();
                // The method issued
                messageReceived = "";
            }

        }

    }

    @Override
    public void onArduinoOpened() {

    }

    private void display(final String message){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String[] messageIds = message.split(":");
                displayFreqType.setText(messageIds[1]);
                displayChanFreq.setText(messageIds[2]);
                displayChanName.setText(messageIds[3]);
                displayChanNr.setText(messageIds[4]);
            }
        });
    }

    public void changeColour(final int ctr){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //Set all sources back to initial color
               radioCard.setCardBackgroundColor(Color.BLUE);
               cdplayerCard.setCardBackgroundColor(Color.BLUE);
               auxCard.setCardBackgroundColor(Color.BLUE);

                //Radio
                if (ctr == 1) radioCard.setCardBackgroundColor(Color.RED);
                //Cd Player
                if (ctr == 2) cdplayerCard.setCardBackgroundColor(Color.RED);
                //Aux
                if (ctr == 3) auxCard.setCardBackgroundColor(Color.RED);
            }
        });
    }

   public void showRadioPopup(View v){
        TextView closeRadioPopup;
        radioPopup.setContentView(R.layout.radio3x3grid);
        closeRadioPopup = (TextView) radioPopup.findViewById(R.id.exit_popup);
        closeRadioPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioPopup.dismiss();
            }
        });
        radioPopup.show();
    }
}


编辑:我附上了整个代码。

第二次编辑:我已经修改了方法的参数。现在应用程序崩溃了。日志:

11-12 16:22:18.598 23257-23346/com.example.multimediainterfacev11 E/AndroidRuntime: FATAL EXCEPTION: Thread-3827
    Process: com.example.multimediainterfacev11, PID: 23257
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:209)
        at android.os.Handler.<init>(Handler.java:123)
        at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:3757)
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:4084)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:306)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
        at android.app.Dialog.show(Dialog.java:325)
        at com.example.multimediainterfacev11.MainActivity.showRadioPopup(MainActivity.java:152)
        at com.example.multimediainterfacev11.MainActivity.onArduinoMessage(MainActivity.java:95)
        at me.aflak.arduino.Arduino.onReceivedData(Arduino.java:155)
        at com.felhr.usbserial.UsbSerialDevice$WorkerThread.onReceivedData(UsbSerialDevice.java:280)
        at com.felhr.usbserial.UsbSerialDevice$WorkerThread.run(UsbSerialDevice.java:254)
11-12 16:22:18.638 23257-23257/com.example.multimediainterfacev11 D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{e0491c8 token=android.os.BinderProxy@50d3261 {com.example.multimediainterfacev11/com.example.multimediainterfacev11.MainActivity}}
11-12 16:22:18.730 23257-23257/com.example.multimediainterfacev11 D/ActivityThread: ACT-PAUSE_ACTIVITY_FINISHING handled : 0 / android.os.BinderProxy@50d3261
11-12 16:22:18.875 23257-23333/com.example.multimediainterfacev11 D/IMGSRV: gralloc_unregister_buffer:1440: ID=10288 ref=0
11-12 16:22:18.876 23257-23333/com.example.multimediainterfacev11 D/IMGSRV: gralloc_unregister_buffer:1440: ID=10287 ref=0
11-12 16:22:18.876 23257-23333/com.example.multimediainterfacev11 D/IMGSRV: gralloc_unregister_buffer:1440: ID=10280 ref=0
11-12 16:22:18.876 23257-23333/com.example.multimediainterfacev11 D/IMGSRV: gralloc_unregister_buffer:1440: ID=10267 ref=0
11-12 16:22:19.026 23257-23257/com.example.multimediainterfacev11 D/WindowClient: Remove from mViews: com.android.internal.policy.PhoneWindow$DecorView{ada1c5b V.E...... R......D 0,0-1920,1080}, this = android.view.WindowManagerGlobal@2ec4334
11-12 16:22:19.026 23257-23257/com.example.multimediainterfacev11 D/ActivityThread: ACT-DESTROY_ACTIVITY handled : 1 / android.os.BinderProxy@50d3261
11-12 16:22:23.983 23257-23266/com.example.multimediainterfacev11 I/System: FinalizerDaemon: finalize objects = 1

标签: androidmethodsviewparameters

解决方案


推荐阅读