1

我正在尝试编写允许使用 JavaOSC 库发送和接收 OSC 消息的示例 Android 应用程序。在示例中,我们发送带有 OSC 消息的简单 Android seekBar 的当前值。

LogCat 说:

09-03 04:56:25.695: I/System.out(1235): can not send

这是我的示例代码,不幸的是它不起作用(似乎我在尝试发送消息时出现异常)。也许有人知道这里出了什么问题?

我是Android开发的新手,谢谢所有建议...

 package com.remote.secondtest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;





public class MainActivity extends Activity implements OnSeekBarChangeListener{

    private SeekBar myBar;
    private TextView textProgress, textAction;
    public OSCPortOut sender = null;
    private OSCPortIn  receiver;
    public InetAddress targetIP;
    int port = 5001;
    protected int sliderValue;


    @Override
    public void onCreate(Bundle savedInstanceState) {

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

        myBar = (SeekBar)findViewById(R.id.seekBar1);
        myBar.setOnSeekBarChangeListener(this);        
        textProgress = (TextView)findViewById(R.id.textViewProgress);
        textAction = (TextView)findViewById(R.id.textViewAction);

    } 

    public void setConnection(){
        try {
            targetIP = InetAddress.getByName("192.168.0.104");
            //targetIP = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {          
            e.printStackTrace();        }        

        try {
            sender = new OSCPortOut(targetIP, 4444); //------set up outgoing ------
        } catch (SocketException e) {
            e.printStackTrace();
        }

        /*try {                                     //------set up incoming-------
            receiver = new OSCPortIn(4444);
        } catch (SocketException e) {
            e.printStackTrace();
        } */ 

    }


    @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 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textProgress.setText("The value is: " + progress);
        textAction.setText("changing");

        sliderValue = progress;
        sendMyOscMessage();
        //receiveMyOscMessage();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {     
        textAction.setText("starting to track touch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        seekBar.setSecondaryProgress(seekBar.getProgress());
        textAction.setText("ended tracking touch");     

    }

    public void sendMyOscMessage(){
        Object args [] = new Object[2];
        args[0] = "Current_Value_is: ";
        args[1] = sliderValue;
        OSCMessage msg = new OSCMessage("/test_from_device", args);

        try {
            sender.send(msg);
            System.out.println("OSC message sent!");
            System.out.println(args[0].toString() + args[1]);
        } catch (Exception e) {
            System.out.println("can not send");
            //e.printStackTrace();
        }

    }

    /*public void receiveMyOscMessage(){
        OSCListener myListener = new OSCListener() {

            @Override
            public void acceptMessage(Date time, OSCMessage message) {

                System.out.println("myMessage_received!");              
                //Object [] args = message.getArguments();
                //String myTitle = args[0].toString();
                //int currentValue = (Integer)args[1];
            }
        }; 

        receiver.addListener("/test_to_device", myListener);
        receiver.startListening();

    }*/


}

UPD: OSCMessage 构造函数已被弃用,但在我将其修改为有效的构造函数后,没有任何更改......

OSCMessage msg = new OSCMessage();
    msg.setAddress("192.168.0.104");
    msg.addArgument(args);

在很多 android 错误日志之间说:

09-03 07:56:00.794: E/AndroidRuntime(2408): 致命异常: main 09-03 07:56:00.794: E/AndroidRuntime(2408): java.lang.NullPointerException 09-03 07:56:00.794: E/AndroidRuntime(2408): 在 com.remote.secondtest.MainActivity.sendMyOscMessage(MainActivity.java:132) 09-03 07:56:00.794: E/AndroidRuntime(2408): 在 com.remote.secondtest.MainActivity.onProgressChanged (MainActivity.java:93) 09-03 07:56:00.794: E/AndroidRuntime(2408): 在 android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)

4

1 回答 1

0

您需要访问 Internet 的权限。

由于行号与您的错误不匹配,我无法确定,但我猜这是最常见的错误,导致“无法发送”。

于 2014-09-12T11:27:45.423 回答