我的问题如下:我有一个活动activity transport
。在此活动中,我有一个按钮Auto
,可以打开新活动:activity_auto
。每当我点击此按钮时,第一个活动activity transport
将再次打开。我认为没有错误显示,引用也没问题。
我附上了我的代码段
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>
答案 0 :(得分:0)
试试这个
Intent it=new Intent(getApplicationContext(),activity_auto.class);
startActivity(it)
您也可以尝试
getBaseContext()
如果无效,请使用
activity_transport.this.startActivity
而不是
startActivity
答案 1 :(得分:0)
您需要添加一个Listener。请尝试下面的代码
btnAuto.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.getId() == R.id.Auto) {
startActivity(new Intent(activity_transport.this, activity_auto.class));
}
}
});