我正在websocket通信。 從android設備(客戶端)到基於linux的PC(服務器)。 我成功地將websocket連接到服務器。但問題是我發送數據失敗(字符串值)Android - NullPointerException錯誤
有一個傳送帶視圖與四個產品。所以,當我點擊product0的照片時,我將字符串設置爲「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!");
}
});
}
是否有可能「擴展片段」,使得錯誤?..是發生像下面 錯誤..當我點擊發生
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)
錯誤照片發送字符串值。
你在哪裏用mConnection連接? –
'newInstance()'的最後一行應該替換爲:'Myoffers_Fragment result = new Myoffers_Fragment(); result.setArguments(b)中;返回結果;'...因爲你似乎返回一個帶有空參數列表的片段。此外,該錯誤在'WebSocketConnection.sendTextMessage()'中拋出。你可以發佈該代碼嗎? – gunar
@ gunar你想讓我發佈哪個代碼?.. Myoffers_Fragment是代碼頂部 – user2500696