按钮无法启动新活动,而是返回启动器活动 - android a

时间:2018-02-22 10:58:25

标签: java android-activity android-button opensuse

大家好...... 我是Android应用程序开发的新手.. 目前,我正在与BITalino Android API合作开发自己的Biosignal应用程序。 当我按下开始按钮时我现在面临的问题...打算转到下一个活动,它将我带回启动器活动(主要活动)。没有错误或警告发出..它只是不按照它应该的方式工作

以下是代码
非常感谢你提前.....

的manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Holo.Light">
    <activity
        android:name=".ScanActivity"
        android:label="Anhalt BITadroid">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DeviceActivity" />
    <activity android:name=".BiosignalDisplay"></activity>
</application>

针对Device_Activity(MainActivity)的onClick()片段

public class DeviceActivity extends Activity implements OnBITalinoDataAvailable, View.OnClickListener {

private final String TAG = this.getClass().getSimpleName();

public final static String EXTRA_DEVICE = "info.plux.pluxapi.sampleapp.DeviceActivity.EXTRA_DEVICE";
public final static String FRAME = "info.plux.pluxapi.sampleapp.DeviceActivity.Frame";

private BluetoothDevice bluetoothDevice;

private BITalinoCommunication bitalino;
private boolean isBITalino2 = false;


private Handler handler;

private States currentState = States.DISCONNECTED;

private boolean isUpdateReceiverRegistered = false;

/*
 * UI elements
 */
private TextView nameTextView;
private TextView addressTextView;
private TextView elapsedTextView;
private TextView stateTextView;

private Button connectButton;
private Button disconnectButton;
private Button startButton;
private Button stopButton;

private LinearLayout bitalinoLinearLayout;
private Button stateButton;
private RadioButton digital1RadioButton;
private RadioButton digital2RadioButton;
private RadioButton digital3RadioButton;
private RadioButton digital4RadioButton;
private Button triggerButton;
private SeekBar batteryThresholdSeekBar;
private Button batteryThresholdButton;
private SeekBar pwmSeekBar;
private Button pwmButton;
private TextView resultsTextView;

private boolean isDigital1RadioButtonChecked = false;
private boolean isDigital2RadioButtonChecked = false;

private float alpha = 0.25f;

/*
 * Test with 2 device
 */
//    private BITalinoCommunication bitalino2;
//    private String identifierBITalino2 = "20:16:07:18:15:94";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(getIntent().hasExtra(EXTRA_DEVICE)){
        bluetoothDevice = getIntent().getParcelableExtra(EXTRA_DEVICE);
    }


    initView();
    setUIElements();

    handler = new Handler(getMainLooper()){
      @Override
      public void handleMessage(Message msg) {
          Bundle bundle = msg.getData();
          BITalinoFrame frame = bundle.getParcelable(FRAME);

          Log.d(TAG, frame.toString());

          if(frame != null){ //BITalino
              resultsTextView.setText(frame.toString());
          }
      }
    };
}

@Override
protected void onResume() {
    super.onResume();

    registerReceiver(updateReceiver, makeUpdateIntentFilter());
    isUpdateReceiverRegistered = true;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if(isUpdateReceiverRegistered) {
        unregisterReceiver(updateReceiver);
        isUpdateReceiverRegistered = false;
    }

    if(bitalino != null){
        bitalino.closeReceivers();
        try {
            bitalino.disconnect();
        } catch (BITalinoException e) {
            e.printStackTrace();
        }
    }

//        if(bitalino2 != null){
//            bitalino2.closeReceivers();
//            try {
//                bitalino2.disconnect();
//            } catch (BITalinoException e) {
//                e.printStackTrace();
//            }
//        }
    }



/*
 * UI elements
 */
private void initView(){
    nameTextView = (TextView) findViewById(R.id.device_name_text_view);
    addressTextView = (TextView) findViewById(R.id.mac_address_text_view);
    elapsedTextView = (TextView) findViewById(R.id.elapsed_time_Text_view);
    stateTextView = (TextView) findViewById(R.id.state_text_view);

    connectButton = (Button) findViewById(R.id.connect_button);
    disconnectButton = (Button) findViewById(R.id.disconnect_button);
    startButton = (Button) findViewById(R.id.start_button);
    stopButton = (Button) findViewById(R.id.stop_button);

    //bitalino ui elements
    bitalinoLinearLayout = (LinearLayout) findViewById(R.id.bitalino_linear_layout);
    stateButton = (Button) findViewById(R.id.state_button);
    digital1RadioButton = (RadioButton) findViewById(R.id.digital_1_radio_button);
    digital2RadioButton = (RadioButton) findViewById(R.id.digital_2_radio_button);
    digital3RadioButton = (RadioButton) findViewById(R.id.digital_3_radio_button);
    digital4RadioButton = (RadioButton) findViewById(R.id.digital_4_radio_button);
    triggerButton = (Button) findViewById(R.id.trigger_button);
    batteryThresholdSeekBar = (SeekBar) findViewById(R.id.battery_threshold_seek_bar);
    batteryThresholdButton = (Button) findViewById(R.id.battery_threshold_button);
    pwmSeekBar = (SeekBar) findViewById(R.id.pwm_seek_bar);
    pwmButton = (Button) findViewById(R.id.pwm_button);
    resultsTextView = (TextView) findViewById(R.id.results_text_view);
}

private void setUIElements(){
    if(bluetoothDevice.getName() == null){
        nameTextView.setText("BITalino");
    }
    else {
        nameTextView.setText(bluetoothDevice.getName());
    }
    addressTextView.setText(bluetoothDevice.getAddress());
    stateTextView.setText(currentState.name());

    Communication communication = Communication.getById(bluetoothDevice.getType());
    Log.d(TAG, "Communication: " + communication.name());
    if(communication.equals(Communication.DUAL)){
        communication = Communication.BLE;
    }

    bitalino = new        BITalinoCommunicationFactory().getCommunication(communication,this, this);
//        bitalino2 = new BITalinoCommunicationFactory().getCommunication(communication,this, this);

    connectButton.setOnClickListener(this);
    disconnectButton.setOnClickListener(this);
    startButton.setOnClickListener(this);
    stopButton.setOnClickListener(this);
    stateButton.setOnClickListener(this);
    digital1RadioButton.setOnClickListener(this);
    digital2RadioButton.setOnClickListener(this);
    digital3RadioButton.setOnClickListener(this);
    digital4RadioButton.setOnClickListener(this);
    triggerButton.setOnClickListener(this);
    batteryThresholdButton.setOnClickListener(this);
    pwmButton.setOnClickListener(this);
}

/*
 * Local Broadcast
 */
private final BroadcastReceiver updateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if(ACTION_STATE_CHANGED.equals(action)){
            String identifier = intent.getStringExtra(IDENTIFIER);
            States state = States.getStates(intent.getIntExtra(EXTRA_STATE_CHANGED, 0));

            Log.i(TAG, identifier + " -> " + state.name());

            stateTextView.setText(state.name());

            switch (state){
                case NO_CONNECTION:
                    break;
                case LISTEN:
                    break;
                case CONNECTING:
                    break;
                case CONNECTED:
                    break;
                case ACQUISITION_TRYING:
                    break;
                case ACQUISITION_OK:
                    break;
                case ACQUISITION_STOPPING:
                    break;
                case DISCONNECTED:
                    break;
                case ENDED:
                    break;

            }
        }
        else if(ACTION_DATA_AVAILABLE.equals(action)){
            if(intent.hasExtra(EXTRA_DATA)){
                Parcelable parcelable = intent.getParcelableExtra(EXTRA_DATA);
                if(parcelable.getClass().equals(BITalinoFrame.class)){ //BITalino
                    BITalinoFrame frame = (BITalinoFrame) parcelable;
                    resultsTextView.setText(frame.toString());
                }
            }
        }
        else if(ACTION_COMMAND_REPLY.equals(action)){
            String identifier = intent.getStringExtra(IDENTIFIER);

            if(intent.hasExtra(EXTRA_COMMAND_REPLY) && (intent.getParcelableExtra(EXTRA_COMMAND_REPLY) != null)){
                Parcelable parcelable = intent.getParcelableExtra(EXTRA_COMMAND_REPLY);
                if(parcelable.getClass().equals(BITalinoState.class)){ //BITalino
                    Log.d(TAG, ((BITalinoState)parcelable).toString());
                    resultsTextView.setText(parcelable.toString());
                }
                else if(parcelable.getClass().equals(BITalinoDescription.class)){ //BITalino
                    isBITalino2 = ((BITalinoDescription)parcelable).isBITalino2();
                    resultsTextView.setText("isBITalino2: " + isBITalino2 + "; FwVersion: " + String.valueOf(((BITalinoDescription)parcelable).getFwVersion()));

//                        if(identifier.equals(identifierBITalino2) &&     bitalino2 != null){
//                            try {
//                                bitalino2.start(new int[]{0,1,2,3,4,5},     1);
//                            } catch (BITalinoException e) {
//                                e.printStackTrace();
//                            }
//                        }
                }
            }
        }
    }
};

private IntentFilter makeUpdateIntentFilter() {
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ACTION_STATE_CHANGED);
    intentFilter.addAction(ACTION_DATA_AVAILABLE);
    intentFilter.addAction(ACTION_EVENT_AVAILABLE);
    intentFilter.addAction(ACTION_DEVICE_READY);
    intentFilter.addAction(ACTION_COMMAND_REPLY);
    return intentFilter;
}

/*
 * Callbacks
 */

@Override
public void onBITalinoDataAvailable(BITalinoFrame bitalinoFrame) {
    Message message = handler.obtainMessage();
    Bundle bundle = new Bundle();
    bundle.putParcelable(FRAME, bitalinoFrame);
    message.setData(bundle);
    handler.sendMessage(message);
}


@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.connect_button:
            try {
                bitalino.connect(bluetoothDevice.getAddress());
            } catch (BITalinoException e) {
                e.printStackTrace();
            }

            break;
        case R.id.disconnect_button:
            try {
                bitalino.disconnect();
            } catch (BITalinoException e) {
                e.printStackTrace();
            }

           break;

        case R.id.start_button:{
            Intent Recordingintent = new Intent(getApplicationContext(), BiosignalDisplay.class);
            startActivity(Recordingintent);}
            break;

        case R.id.stop_button:
            Intent exit = new Intent(this, ScanActivity.class);
            startActivity(exit);
            break;
    }
}
}

main.xml中enter code here

1 个答案:

答案 0 :(得分:0)

这些问题实际上无法回答所提供的信息,因此我将根据评论中提供的上下文提供有关如何查找这些类型问题和实际解决方案的一般性指导。

如果您的应用在您不期望的情况下关闭,通常的原因是未被​​捕获RuntimeException。与"checked exceptions"相反,这些不必被try-catch包围,编译器就会感到高兴。如果他们没有被抓住,他们将终止程序/ App。

修复这些异常的第一步是检查Android日志(Log Cat)以查找异常和堆栈跟踪。在Android Studio中,日志输出位于左下角。

如果Log忙于处理很多事情,您可以按类型和应用程序对其进行过滤。为了找出错误,我们通过我们的App和&#34;错误&#34;对数级。

问题(来自评论)的实际例外是:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

这(非常可能)表示BiosignalDisplay - 活动从Android Support Library延伸AppCompactActivity,但AndroidManifest.xml中的条目未设置支持 - 活动的图书馆主题。

可以通过将theme - 标签或<activity> - 标记上的<application> - 属性设置为Theme.AppCompat下的任何主题来解决此问题。 Theme.AppCompat.Light.NoActionBar

有关详细信息,请参阅:You need to use a Theme.AppCompat theme (or descendant) with this activity