Android - NullPointerException错误

时间:2013-06-19 10:31:30

标签: android nullpointerexception websocket autobahn

我正在开展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)

单击照片发送字符串值时发生错误。

1 个答案:

答案 0 :(得分:2)

错误发生在onClick此处:

mConnection.sendTextMessage(id1);

看起来您已在顶部声明mConnection,但未建立任何连接。

查看docs,您需要在使用.connect()之前致电mConnection

WebSocketConnection.java的第137行是:

public void sendTextMessage(String payload) {
    mWriter.forward(new WebSocketMessage.TextMessage(payload));
}
在您致电mWriter之前,

.connect()为空。 Source code

因此,在使用.connect()对象之前,请确保您具有有效的连接(通过调用mConnection)。