onResume停止我的申请



我的应用程序使用FTDI芯片获得串行USB输入,我设法做到了。当我按下一个按钮,我得到3个输入浮点型数字显示在我的应用程序。以下是onClick:

public void onReadClick(View view) throws UnsupportedEncodingException {
          // [FTDriver] Create Read Buffer
            byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte
           // [FTDriver] Read from USB Serial
            len = mSerial.read(rbuf);
            for(i=0; i<len; i++) {
                                  mText1.append((char) rbuf[i]);
                                 }
            Text = mText1.toString();
            String[] Splited = Text.split("#");

            Long r = Long.parseLong(Splited[1], 16);   
            Float f = Float.intBitsToFloat(r.intValue()); 
            d1 = f.toString(); 
            val.setText(d1);

            Long r2 = Long.parseLong(Splited[2], 16);   
            Float f2 = Float.intBitsToFloat(r2.intValue()); 
            d2 = f2.toString(); 
            val2.setText(d2);

            Long r3 = Long.parseLong(Splited[3], 16);   
            Float f3 = Float.intBitsToFloat(r3.intValue()); 
            d3 = f3.toString(); 
            val3.setText(d3);

            Monitor.setText(Splited[1]);
            Monitor2.setText(Splited[2]);
            Monitor3.setText(Splited[3]);
  }

它非常好,直到我想让我的应用程序在按钮onClick之外执行这些步骤,所以我把它放在外面(我是java的新手):

package com.application.i;

import java.io.UnsupportedEncodingException;
import jp.ksksue.driver.serial.FTDriver;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class FTDI extends Activity{

    // [FTDriver] Object
    FTDriver mSerial;
    // [FTDriver] Permission String
    private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";
    Button btnRead;
    TextView Monitor,Monitor2,Monitor3;
    TextView val,val2,val3;
    TextView indicator;
    StringBuffer mText1 = new StringBuffer();
    int i,len;
    String Text=null;
    boolean doIT = true;
    String number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ftdisub);
    btnRead = (Button) findViewById(R.id.btnRead);
    Monitor = (TextView)findViewById(R.id.onitor);
    Monitor2 = (TextView)findViewById(R.id.onitor2);
    Monitor3 = (TextView)findViewById(R.id.onitor3);
    val = (TextView)findViewById(R.id.dec);
    val2 = (TextView)findViewById(R.id.dec2);
    val3 = (TextView)findViewById(R.id.dec3);
    indicator = (TextView)findViewById(R.id.indicator);
    // [FTDriver] Create Instance
    mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));

    // [FTDriver] setPermissionIntent() before begin()
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mSerial.setPermissionIntent(permissionIntent);
    // [FTDriver] Open USB Serial
    if(mSerial.begin(FTDriver.BAUD115200)) {
                                            btnRead.setEnabled(true);
                                            Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
                                            } else {
                                                    Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
                                                    }


    }
    @Override 
    protected void onResume() {
        if (doIT==true){
            // [FTDriver] Create Read Buffer
            byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte
           // [FTDriver] Read from USB Serial
            len = mSerial.read(rbuf);
            for(i=0; i<len; i++) {
                                  mText1.append((char) rbuf[i]);
                                 }
            Text = mText1.toString();
            String[] Splited = Text.split("#");

            Long r = Long.parseLong(Splited[1], 16);   
            Float f = Float.intBitsToFloat(r.intValue()); 
            d1 = f.toString(); 
            val.setText(d1);

            Long r2 = Long.parseLong(Splited[2], 16);   
            Float f2 = Float.intBitsToFloat(r2.intValue()); 
            d2 = f2.toString(); 
            val2.setText(d2);

            Long r3 = Long.parseLong(Splited[3], 16);   
            Float f3 = Float.intBitsToFloat(r3.intValue()); 
            d3 = f3.toString(); 
            val3.setText(d3);

            Monitor.setText(Splited[1]);
            Monitor2.setText(Splited[2]);
            Monitor3.setText(Splited[3]);
            doIT=false;
        }

    };
    @Override
    public void onDestroy() {
                            super.onDestroy();
                            // [FTDriver] Close USB Serial
                            mSerial.end();
                            }
    public void onReadClick(View view) throws UnsupportedEncodingException {
       doIT=true;



      }
   }

我只想在没有Button的情况下阅读一次,并且对于每个添加时间,当我按下按钮时,应用程序将再次执行该过程。最主要的是我希望它在按钮之外,特别是在FTDI活动被激活时做一次。

当我点击应用程序主屏幕上的按钮时,FTDI活动被激活。

问题:当我点击主屏幕上的FTDI按钮时,它应该启动FTDI活动并显示3个浮动数字,而不是应用程序以错误和关闭消息完成。

LogCat:

05-15 11:04:46.855: E/AndroidRuntime(9598): FATAL EXCEPTION: main
05-15 11:04:46.855: E/AndroidRuntime(9598): java.lang.RuntimeException: Unable to resume activity {com.application.i/com.application.i.FTDI}: java.lang.NullPointerException
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.os.Looper.loop(Looper.java:137)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.main(ActivityThread.java:4898)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at java.lang.reflect.Method.invoke(Method.java:511)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at dalvik.system.NativeStart.main(Native Method)
05-15 11:04:46.855: E/AndroidRuntime(9598): Caused by: java.lang.NullPointerException
05-15 11:04:46.855: E/AndroidRuntime(9598):     at jp.ksksue.driver.serial.FTDriver.read(FTDriver.java:271)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at jp.ksksue.driver.serial.FTDriver.read(FTDriver.java:257)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at com.application.i.FTDI.onResume(FTDI.java:83)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1188)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.Activity.performResume(Activity.java:5280)
05-15 11:04:46.855: E/AndroidRuntime(9598):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
05-15 11:04:46.855: E/AndroidRuntime(9598):     ... 12 more

p。S:这个FTDI活动运行良好:

public class FTDI extends Activity{

    // [FTDriver] Object
    FTDriver mSerial;
    // [FTDriver] Permission String
    private static final String ACTION_USB_PERMISSION = "jp.ksksue.tutorial.USB_PERMISSION";
    Button btnRead;
    TextView Monitor,Monitor2,Monitor3;
    TextView val,val2,val3;
    TextView indicator;
    StringBuffer mText1 = new StringBuffer();
    int i,len;
    String Text=null;
    boolean doIT = true;
    String number=null,number2=null,number3=null, hadar1=null, hadar2=null, hadar3=null, d1=null, d2=null, d3=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ftdisub);
    btnRead = (Button) findViewById(R.id.btnRead);
    Monitor = (TextView)findViewById(R.id.onitor);
    Monitor2 = (TextView)findViewById(R.id.onitor2);
    Monitor3 = (TextView)findViewById(R.id.onitor3);
    val = (TextView)findViewById(R.id.dec);
    val2 = (TextView)findViewById(R.id.dec2);
    val3 = (TextView)findViewById(R.id.dec3);
    indicator = (TextView)findViewById(R.id.indicator);
    // [FTDriver] Create Instance
    mSerial = new FTDriver((UsbManager)getSystemService(Context.USB_SERVICE));

    // [FTDriver] setPermissionIntent() before begin()
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mSerial.setPermissionIntent(permissionIntent);
    // [FTDriver] Open USB Serial
    if(mSerial.begin(FTDriver.BAUD115200)) {
                                            btnRead.setEnabled(true);
                                            Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();
                                            } else {
                                                    Toast.makeText(this, "cannot connect", Toast.LENGTH_SHORT).show();
                                                    }


    }

    @Override
    public void onDestroy() {
                            super.onDestroy();
                            // [FTDriver] Close USB Serial
                            mSerial.end();
                            }
    public void onReadClick(View view) throws UnsupportedEncodingException {


           // [FTDriver] Create Read Buffer
           byte[] rbuf = new byte[27];   // 1byte <--slow-- [Transfer Speed] --fast--> 4096 byte
          // [FTDriver] Read from USB Serial
           len = mSerial.read(rbuf);
           for(i=0; i<len; i++) {
                                 mText1.append((char) rbuf[i]);
                                }
           Text = mText1.toString();
           String[] Splited = Text.split("#");

           Long r = Long.parseLong(Splited[1], 16);   
           Float f = Float.intBitsToFloat(r.intValue()); 
           d1 = f.toString(); 
           val.setText(d1);

           Long r2 = Long.parseLong(Splited[2], 16);   
           Float f2 = Float.intBitsToFloat(r2.intValue()); 
           d2 = f2.toString(); 
           val2.setText(d2);

           Long r3 = Long.parseLong(Splited[3], 16);   
           Float f3 = Float.intBitsToFloat(r3.intValue()); 
           d3 = f3.toString(); 
           val3.setText(d3);

           Monitor.setText(Splited[1]);
           Monitor2.setText(Splited[2]);
           Monitor3.setText(Splited[3]);


      }
   }

您必须检查活动的生命周期。我猜你的mSerial在on resume方法被调用时没有初始化试着在on start

中进行操作

相关内容

  • 没有找到相关文章

最新更新