【发布时间】: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