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

button fails to launch the new activity instead it goes back to launcher activity- android a

大家好... 我是 Android 应用程序开发的新手 .. 目前我正在与 BITalino Android API 合作开发我自己的 Biosignal 应用程序。 当我按下开始按钮时我现在面临的问题是……它打算移动到下一个 Activity 它带我回到启动器 Activity(Main Activity).No发出错误或警告..它只是没有按预期工作

下面是代码
非常感谢您.....

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.xmlenter code here

这个问题真的不能用提供的信息来回答,所以我将根据评论中提供的上下文给出一些关于如何找到这些类型的问题和实际解决方案的一般指导。


如果您的应用程序在您不希望的情况下关闭,通常的原因是未捕获 RuntimeException。与 "checked exceptions" 相比,这些不必被 try-catch 包围以使编译器满意。如果他们没有被抓住,他们将终止 program/App.

修复这些异常的第一步是检查 Android 日志 (Log Cat) 中的异常和 stack-trace。在 Android Studio 中,日志输出位于左下角。

如果日志非常忙于处理很多事情,您可以按类型和应用程序过滤它。为了找出错误,我们通过我们的应用程序和 "Error" log-level.

进行过滤

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

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

这(很可能)意味着 BiosignalDisplay-activity 从 Android Support Library 扩展了 AppCompactActivity,但 AndroidManifest.xml 中的条目没有t 为 activity 设置 support-library 主题。

这可以通过将特定 <activity> 标签或 <application> 标签上的 theme 属性设置为 Theme.AppCompat 下的任何主题来解决,例如 Theme.AppCompat.Light.NoActionBar.

有关这方面的更多信息,请参阅:You need to use a Theme.AppCompat theme (or descendant) with this activity