我正在进行websocket通信,从安卓设备(客户端)到基于Linux的PC(服务器)。我成功连接了websocket到服务器,但问题是发送数据(字符串值)时失败了。
有一个旋转木马视图,其中有四个产品。因此,当我点击产品0的照片时,我将字符串设置为“product0”,并将此字符串值发送到服务器。我正在使用Autobahn库。
代码在这里:
当我点击照片发送字符串值时,出现了错误。
有一个旋转木马视图,其中有四个产品。因此,当我点击产品0的照片时,我将字符串设置为“product0”,并将此字符串值发送到服务器。我正在使用Autobahn库。
代码在这里:
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!");
}
});
}
可能是"extends Fragment"导致了错误?..错误信息如下..
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)
当我点击照片发送字符串值时,出现了错误。