我正在研究 websocket 通信。从 android 设备(客户端)到基于 linux 的 PC(服务器)。我成功地将 websocket 连接到服务器。但问题是我发送数据失败(字符串值)
有四个产品的轮播视图。因此,当我单击 product0 的照片时,我将字符串设置为“product0”并将此字符串值发送到服务器。我正在使用高速公路库。
代码在这里
import de.tavendo.autobahn.WebSocketConnection;
public class Myoffers_Fragment extends Fragment {
private static final String TAG = "Philips";
private final WebSocketConnection mConnection = new WebSocketConnection();
public static Fragment newInstance(Myoffers context, int pos, float scale)
{
Bundle b = new Bundle();
b.putInt("pos", pos);
b.putFloat("scale", scale);
return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false);
int pos = this.getArguments().getInt("pos");
TextView tv = (TextView) l.findViewById(R.id.text);
tv.setText("Product " + pos);
ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);
if (pos == 0) {
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setOnClickListener(new ImageButton.OnClickListener(){
public void onClick(View v){
String id1 = "Product0";
Log.d(TAG, "Current product is : " + id1);
mConnection.sendTextMessage(id1);
Log.d(TAG, id1 + "is sent to server!");
}
});
}
“扩展片段”是否可能会出错?..错误发生如下..
06-19 12:02:01.310: E/AndroidRuntime(2712): FATAL EXCEPTION: main
06-19 12:02:01.310: E/AndroidRuntime(2712): java.lang.NullPointerException
06-19 12:02:01.310: E/AndroidRuntime(2712): at de.tavendo.autobahn.WebSocketConnection.sendTextMessage(WebSocketConnection.java:137)
06-19 12:02:01.310: E/AndroidRuntime(2712): at com.example.philips.Myoffers_Fragment$1.onClick(Myoffers_Fragment.java:56)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.view.View.performClick(View.java:3511)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.view.View$PerformClick.run(View.java:14105)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.os.Handler.handleCallback(Handler.java:605)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.os.Looper.loop(Looper.java:137)
06-19 12:02:01.310: E/AndroidRuntime(2712): at android.app.ActivityThread.main(ActivityThread.java:4446)
06-19 12:02:01.310: E/AndroidRuntime(2712): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 12:02:01.310: E/AndroidRuntime(2712): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 12:02:01.310: E/AndroidRuntime(2712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 12:02:01.310: E/AndroidRuntime(2712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 12:02:01.310: E/AndroidRuntime(2712): at dalvik.system.NativeStart.main(Native Method)
当我单击照片发送字符串值时发生错误。