我正在尝试编写示例Android应用程序,该应用程序允许使用JavaOSC库发送和接收OSC消息。在示例中,我们使用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):致命异常:主要 09-03 07:56:00.794:E / AndroidRuntime(2408):java.lang.NullPointerException 09-03 07:56:00.794:E / AndroidRuntime(2408):at com.remote.secondtest.MainActivity.sendMyOscMessage(MainActivity.java:132) 09-03 07:56:00.794:E / AndroidRuntime(2408):at com.remote.secondtest.MainActivity.onProgressChanged(MainActivity.java:93) 09-03 07:56:00.794:E / AndroidRuntime(2408):在android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)
答案 0 :(得分:0)
您需要访问Internet的权限。
由于行号与您的错误不符,我无法确定,但我猜这是最常见的错误,导致"无法发送"。