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

Posted

技术标签:

【中文标题】按钮无法启动新活动,而是返回到启动器活动 - android a【英文标题】:button fails to launch the new activity instead it goes back to launcher activity- android a 【发布时间】:2018-02-22 10:58:24 【问题描述】:

大家好... 我是 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.xmlenter code here

【问题讨论】:

检查 LogCat 输出。那里应该有一个例外。还要检查你的&lt;activity&gt;-manifest 条目,看看这些活动是否真的在正确的包中。 感谢 lucas 的评论 .. 但我在代码中看不到任何异常情况 .... 这就是为什么我想知道问题的原因是什么 ... 我说的不是你的代码,而是Andorid Log。检查那个。没有看到更多代码:确保您没有在布局 XML 中注册 click-Listener。还要检查您正在启动的活动的代码。如果 Log 中没有任何内容,也许你有一个 try-catch 可以吞下一些异常? 感谢 Lukas 的支持 .. 我试图检查 logcat ... 关于我的活动 .. 我可以看到这些错误........ java.lang.IllegalStateException: You需要在此活动中使用 Theme.AppCompat 主题(或后代)。 ......和............java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代)与此活动。 谢谢 Lukas ...我将 Extend AppCompactActivity 更改为 Activity 并且它可以工作.. 感谢您的支持 【参考方案1】:

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


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

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

如果日志很忙,有很多事情要做,您可以按类型和应用程序对其进行过滤。为了找出错误,我们按我们的应用程序和“错误”日志级别进行过滤。


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

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

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

可以通过将特定&lt;activity&gt;-tag 上的theme-attribute 或&lt;application&gt;-tag 上的Theme.AppCompat 下的任何主题设置为Theme.AppCompat 下的任何主题,例如Theme.AppCompat.Light.NoActionBar

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

【讨论】:

谢谢你 Lukas ...我改变了,它按照你的解释工作......我将公共类 BiosignalDisplay extends AppcompactActivity 更改为公共类 BiosignalDisplay extends Activity .....非常感谢支持

以上是关于按钮无法启动新活动,而是返回到启动器活动 - android a的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 中的新活动无法启动

启动新活动并清除除家庭活动外的先前活动

带有按钮的 Eclipse android 主菜单启动新活动(IES)

如何在按钮单击时启动新活动

在操作栏上按“向上导航按钮”并返回到父活动时出错

启动新意图后活动加载缓慢