1

我尝试使用我的 Android 按钮来更改 at_command 的值

如果我调用logcat 会"intOfFloat(float f)"显示btnLeftListener

01-23 16:17:28.610: E/AndroidRuntime(1154): java.lang.NullPointerException

请帮我解决这个问题:)

String at_cmd = "";
FloatBuffer fb;
IntBuffer ib;
int  PORT=5556;
float speed = (float)0.1;
int seq = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    btnLeft=(Button)findViewById(R.id.LeftBtn);
    btnLeft.setOnClickListener(btnLeftListener);
}
public Button.OnClickListener btnLeftListener=
      new Button.OnClickListener(){
      @Override
        public void onClick(View arg0) {
            at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(-speed)+",0,0,0";
    }

};
public int intOfFloat(float f) {
    fb.put(0, f);
    return ib.get(0);
}

日志猫

01-23 16:17:28.591: W/dalvikvm(1154): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-23 16:17:28.610: E/AndroidRuntime(1154): FATAL EXCEPTION: main
01-23 16:17:28.610: E/AndroidRuntime(1154): java.lang.NullPointerException
01-23 16:17:28.610: E/AndroidRuntime(1154):     at com.example.dronedivideandroid.DroneMain.intOfFloat(DroneMain.java:60)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at com.example.dronedivideandroid.DroneMain$1.onClick(DroneMain.java:70)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.view.View.performClick(View.java:4084)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.view.View$PerformClick.run(View.java:16966)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.os.Handler.handleCallback(Handler.java:615)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.os.Looper.loop(Looper.java:137)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-23 16:17:28.610: E/AndroidRuntime(1154):     at dalvik.system.NativeStart.main(Native Method)
01-23 16:17:28.631: W/ActivityManager(163):   Force finishing activity com.example.dronedivideandroid/.DroneMain
01-23 16:17:28.641: W/WindowManager(163): Failure taking screenshot for (246x410) to layer 21010
01-23 16:17:29.150: W/ActivityManager(163): Activity pause timeout for ActivityRecord{4154a830 com.example.dronedivideandroid/.DroneMain}
01-23 16:17:29.351: I/Choreographer(262): Skipped 37 frames!  The application may be doing too much work on its main thread.
01-23 16:17:30.591: I/Process(1154): Sending signal. PID: 1154 SIG: 9
01-23 16:17:30.601: I/ActivityManager(163): Process com.example.dronedivideandroid (pid 1154) has died.
01-23 16:17:30.601: I/WindowState(163): WIN DEATH: Window{414043e0 com.example.dronedivideandroid/com.example.dronedivideandroid.DroneMain paused=false}
01-23 16:17:30.691: W/InputMethodManagerService(163): Got RemoteException sending setActive(false) notification to pid 1154 uid 10047
01-23 16:17:40.091: W/ActivityManager(163): Activity destroy timeout for ActivityRecord{4154a830 com.example.dronedivideandroid/.DroneMain}
01-23 16:19:52.800: D/dalvikvm(248): GC_CONCURRENT freed 383K, 7% free 8537K/9095K, paused 6ms+5ms, total 46ms
01-23 16:23:49.971: E/ThrottleService(163): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
01-23 16:27:27.310: D/dalvikvm(248): GC_CONCURRENT freed 384K, 7% free 8537K/9095K, paused 17ms+6ms, total 56ms
4

3 回答 3

1

您的变量之一未初始化,因此您应确保以下变量具有值:

seq, speed, fb, ib.

于 2013-01-23T08:30:47.743 回答
0

可能这将是正确的答案,因为我在设备上对此进行了测试...!!!!!!

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

 import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
String at_cmd = "";
FloatBuffer fb;
IntBuffer ib;
int  PORT=5556;
float speed = (float)0.1;
int seq = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);
   Button  btnLeft=(Button)findViewById(R.id.button1);
    btnLeft.setOnClickListener(btnLeftListener);
}
public OnClickListener btnLeftListener=
      new OnClickListener(){


    @Override
    public void onClick(View arg0) {
         at_cmd = "AT*PCMD=" + String.valueOf(seq++) + ",1," + intOfFloat(-speed)+",0,0,0";

         System.out.println("Ans----->"+at_cmd);
    }

};
public int intOfFloat(float f) {
    if(fb != null) fb.put(0, f);
    if(ib != null) return ib.get(0);
    else return 0;

}

}

我得到了以下输出......!!!

AT*PCMD=1,1,0,0,0,0。

否则检查它...!!!!!!!

于 2013-01-23T10:00:33.753 回答
0

确保变量fbib在使用它们之前被初始化(不为空)

public int intOfFloat(float f) {
if(fb != null) fb.put(0, f);
if(ib != null) return ib.get(0);
else return 0;
}
于 2013-01-23T08:30:55.320 回答