【问题标题】:Android App - Clicking on Button does not open new activityAndroid 应用程序 - 单击按钮不会打开新活动
【发布时间】:2017-12-03 05:57:19
【问题描述】:

我的问题如下:我有一个活动activity transport。在此活动中,我有一个按钮 Auto,它应该打开新活动:activity_auto。每当我单击此按钮时,第一个活动activity transport 就会再次打开。我认为没有显示任何错误,并且引用还可以。

我附上了我的代码 sn-p

Class: activity_transport

package com.group6.travlhoe;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class activity_transport extends AppCompatActivity implements View.OnClickListener {

Button btnAuto;
private BottomNavigationView bottomNavigationView;


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


    btnAuto = (Button) findViewById(R.id.Auto);
    btnAuto.setOnClickListener(this);


    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item){
            if (item.getItemId()==R.id.menu_start){
                startActivity(new Intent(activity_transport.this, MainActivity.class));
            } else if(item.getItemId()==R.id.menu_allgemein){
                startActivity(new Intent(activity_transport.this, activity_allgemein.class));
            } else if(item.getItemId()==R.id.menu_transport){
                startActivity(new Intent(activity_transport.this, activity_transport.class));
            } else if(item.getItemId()==R.id.menu_rechnung){
                startActivity(new Intent(activity_transport.this, activity_rechnung.class));
            } else if(item.getItemId()==R.id.menu_unterkunft){
                startActivity(new Intent(activity_transport.this, activity_unterkunft.class));
            }
            return true;
        }
    });

    bottomNavigationView.setSelectedItemId(R.id.menu_transport);
}



@Override
public void onClick(View v) {
    if (v.getId() == R.id.Auto) {
        startActivity(new Intent(activity_transport.this, activity_auto.class));
    }
}



}

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/hintergrund">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="0.3" >

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Auto"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:alpha="0.75"
            android:background="@mipmap/buttom_col"
            android:text="Auto"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/Flugzeug"
            android:alpha="0.9"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@mipmap/buttom_col"
            android:text="Flugzeug"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/Taxi"
            android:alpha="0.9"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@mipmap/buttom_col"
            android:text="Taxi"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/Bahn"
            android:alpha="0.75"
            android:text="Bahn"
            android:textStyle="bold"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@mipmap/buttom_col" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="0.3" >

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="top"
            android:background="@drawable/shadow" />


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@color/colorWhite"
            design:menu="@menu/navigation" />

    </FrameLayout>



</LinearLayout>

【问题讨论】:

  • 请分享xml文件-activity_transport.xml
  • 在 OnClick 方法中检查 if 条件得到实际结果。
  • @MohamedMohaideenAH 我如何检查这个?抱歉,我对编程很陌生?
  • 记录 v.getId() 值返回正确。或者删除 if 条件然后尝试。
  • 您是否在manifest 文件中添加了该活动?

标签: java android button android-activity


【解决方案1】:

试试这个

Intent it=new Intent(getApplicationContext(),activity_auto.class);
startActivity(it)

你也可以试试

getBaseContext()

如果没有工作,请使用

activity_transport.this.startActivity

而不是

startActivity

【讨论】:

  • 您好,您的任何建议都不适合我...您还有其他想法吗?
【解决方案2】:

你需要添加一个监听器。试试下面的代码

btnAuto.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (v.getId() == R.id.Auto) {
                startActivity(new Intent(activity_transport.this, activity_auto.class));
            }
        }
});

【讨论】:

  • 但我的代码中已经有一个监听器。在我的 onCreate 方法中查看
  • 如果不需要循环。您已将 onclicklistener 设置为仅 btnAuto。
  • 我在 oncreate 方法中实现了你的代码,但问题是一样的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 2012-10-23
  • 1970-01-01
  • 2020-02-01
  • 2017-12-03
相关资源
最近更新 更多