Witam, piszę apkę, która docelowo ma działać jak joystick/pad. Komunikuje się ona z drugim urządzeniem i jeśli na pierwszym nacisnę "w prawo" to na drugim ma zostać odebrany taki komunikat. Jako, że nigdy wcześniej się androidem nie zajmowałem to na początku rozwiązałem to w taki sposób, że po prostu rozmieściłem na ekranie dużo buttonów dookoła i apka działała bez zarzutu.
Teraz przyszedł czas na pewne zmiany. Chciałbym, żeby moja aplikacja działała tak jak na filmiku tutaj:
Autor tego filmiku dodał również kod na swojej stronie. Postanowiłem, że użyję tego kodu w swojej aplikacji. Jednak kiedy próbują włączyć apkę, to ona automatycznie zostaje wyłączona.
Problem pojawia się, gdy wkleję tę część kodu:
js = new JoyStickClass(getApplicationContext(), layout_joystick, R.drawable.image_button);
js.setStickSize(150, 150);
js.setLayoutSize(500, 500);
js.setLayoutAlpha(150);
js.setStickAlpha(100);
js.setOffset(90);
js.setMinimumDistance(50);
layout_joystick.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
js.drawStick(arg1);
if(arg1.getAction() == MotionEvent.ACTION_DOWN
|| arg1.getAction() == MotionEvent.ACTION_MOVE) {
textView1.setText("X : " + String.valueOf(js.getX()));
textView2.setText("Y : " + String.valueOf(js.getY()));
textView3.setText("Angle : " + String.valueOf(js.getAngle()));
textView4.setText("Distance : " + String.valueOf(js.getDistance()));
int direction = js.get8Direction();
if(direction == JoyStickClass.STICK_UP) {
textView5.setText("Direction : Up");
} else if(direction == JoyStickClass.STICK_UPRIGHT) {
textView5.setText("Direction : Up Right");
} else if(direction == JoyStickClass.STICK_RIGHT) {
textView5.setText("Direction : Right");
} else if(direction == JoyStickClass.STICK_DOWNRIGHT) {
textView5.setText("Direction : Down Right");
} else if(direction == JoyStickClass.STICK_DOWN) {
textView5.setText("Direction : Down");
} else if(direction == JoyStickClass.STICK_DOWNLEFT) {
textView5.setText("Direction : Down Left");
} else if(direction == JoyStickClass.STICK_LEFT) {
textView5.setText("Direction : Left");
} else if(direction == JoyStickClass.STICK_UPLEFT) {
textView5.setText("Direction : Up Left");
} else if(direction == JoyStickClass.STICK_NONE) {
textView5.setText("Direction : Center");
}
}
return true;
}
}
Logi wyglądają następująco:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.bluetoothchat, PID: 10076
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluetoothchat/com.example.android.bluetoothchat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.ViewGroup.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$1200(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.ViewGroup.getLayoutParams()' on a null object reference
at com.example.android.bluetoothchat.JoyStickClass.<init>(JoyStickClass.java:54)
at com.example.android.bluetoothchat.MainActivity.onCreate(MainActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6102)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$1200(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)