下午好,
我正在尝试在我的andorid应用程序中创建一个基本菜单,其中包含5个按钮,每个按钮将您带到另一个表单。我正在尝试创建java来执行此操作但似乎与我的每个按钮一起运行到以下错误
“示例无法解析为变量”
请帮我解决一下我的代码的解决方案,或者是否有一种更简单的方法可以让我执行此菜单,其中包含5个按钮,每个按钮都会以不同的形式执行
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.techblogon.loginexample.MainMenu" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/pic" />
<Button
android:id="@+id/btnFootball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="btnFootball" />
<Button
android:id="@+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnFootball"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="btnHockey" />
<Button
android:id="@+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="btnLacrosse" />
<Button
android:id="@+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="btnCurling" />
<Button
android:id="@+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="btnLogout" />
</RelativeLayout>
这是Java:
package com.techblogon.loginexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
public class MainMenu extends Activity {
public void ButtonOnClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
startActivity(Football);
break;
case R.id.btnHockey:
startActivity(Hockey);
break;
case R.id.btnLacrosse:
startActivity(Lacrosse);
break;
case R.id.btnCurling:
startActivity(Curling);
break;
case R.id.btnLogout:
startActivity(HomeActivity);
break;
}
}
}
答案 0 :(得分:1)
你的错误 - &gt;
您尚未使用setcontentView()
,也未覆盖onCreate()
方法。还有一件事,你没有正确发送intent
。
以下是发送意图的方法
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
答案 1 :(得分:1)
我修改了类和布局。您需要为每个操作创建其他类。
public class MainActivity extends Activity implements Button.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
int id = v.getId();
Intent intent = null;
if(id == R.id.btnHockey) {
intent = new Intent(MainActivity.this, Hockey.class);
} else if(id == R.id.btnCurling) {
intent = new Intent(MainActivity.this, Curling.class);
} else if(id == R.id.btnFootball) {
intent = new Intent(MainActivity.this, Football.class);
} else if(id == R.id.btnLogout) {
intent = new Intent(MainActivity.this, Logout.class);
} else if(id == R.id.btnLacrosse) {
intent = new Intent(MainActivity.this, Lacrosse.class);
}
startActivity(intent);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/btnFootball"
android:layout_below="@id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="onClick" />
<Button
android:id="@+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnFootball"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="onClick" />
<Button
android:id="@+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnHockey"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="onClick" />
<Button
android:id="@+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="onClick" />
<Button
android:id="@+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="onClick" />
答案 2 :(得分:1)
@ Meryl2 当你使用 “” 机器人:的onClick = “btnLogout” “”
然后你应该有相应的方法 public void btnLogout(查看视图{ //你的代码在这里 }
同样适用于代码中的所有按钮
答案 3 :(得分:1)
我想我遇到了你的问题。在每个Button中,您可以设置单击它时将使用的方法的名称:
机器人:的onClick =&#34; btnCurling&#34;
机器人:的onClick =&#34; btnHockey&#34;
...
但这里只有一种方法:
public void ButtonOnClick(View v) {
...
}
一种解决方案是定义每个方法,如下所示:
public class MainMenu extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
}
// it will get there when you click on the "@+id/btnHockey" Button
public void btnHockey(View v) {
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
}
// then you add the other ones btnCurling, ...
}
另一种解决方案是以编程方式设置每个按钮的监听器:
public class MainMenu extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
// This will find your button in the layout you just set
// and then set this Class as your Listener
findViewById(R.id.btnFootball).setOnClickListener(this);
// Then set the listener of all your other buttons
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
case R.id.btnHockey:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
// ...
}
}
}
您还必须删除XML布局文件中的所有android:onClick="btnCurling"
行才能使此解决方案正常工作。
希望它有助于欢呼:)
答案 4 :(得分:0)
试试这段代码;
public class MainMenu extends Activity implements OnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btn_football = (Button)findViewById(R.id.btnFootball);
// add another button here same way
btn_football.setOnClickListener(this);
// add another button here same way
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnFootball:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
case R.id.another button:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
//.......add here another button with same way
}
}
}