可能是一个基本的错误,但我正在努力为我在网上找到的随机开源项目添加其他功能,作为学习使用android的一部分,因此显然希望添加其他活动。我的代码如下,取出不相关的位:
import mun.pakkaus.aateekoo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.util.Log;
public class CalorieCounter extends Activity
{
String GDA;
EditText tx, weightE, heightE, ageE;
TextView counter;
Spinner spin;
String gender;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button2 = (Button) findViewById(R.id.button2);
}
public OnClickListener button2 = new OnClickListener()
{
public void onClick(View arg0)
{
Intent nextScreen = new Intent(getApplicationContext(),
SecondScreenActivity.class);
nextScreen.putExtra("Gender", gender);
Log.e("n", gender);
startActivity(nextScreen);
}
};
}
,第二项活动是:
package my.pack.coursework;
import mun.pakkaus.aateekoo.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondScreenActivity extends Activity
{
public void OnCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
TextView counter;
Intent i = getIntent();
String gender = i.getStringExtra("gender");
Log.e("Second Screen", gender + ".");
counter = (TextView) findViewById(R.id.textView23);
int caloriesBurned = 0; int caloriesConsumed = 0;
EditText consumedE;
EditText burnedE;
String test1, test2;
test1 = getString(R.id.txtBurned);
test2 = getString(R.id.txtConsumed);
try
{
if (test1 != "" && test2 != "")
{
burnedE = (EditText) findViewById(R.id.txtBurned);
caloriesBurned = Integer.parseInt(burnedE.getText().toString().trim());
consumedE = (EditText) findViewById(R.id.txtConsumed);
caloriesConsumed = Integer.parseInt(consumedE.getText().toString().trim());
if(gender.contains("Male") && caloriesConsumed - caloriesBurned > 2000)
{
counter.setText("You are over your GDA of calories");
}
else
{
counter.setText("Don't add offensive text");
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
});
}
}
基本上,为什么我的活动没有转换?
答案 0 :(得分:1)
您已在本地创建了Button,因此在OnCreate()之后会将其销毁。 在类中全局声明,只在OnCreate()中初始化。
Button button2;
onCreate()
{
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
public void onClick(View v)
{
if(v == button2)
{
}
}
答案 1 :(得分:1)
您没有分配OnClickListener,只是创建一个与Button对象同名的OnClickListener。
正确使用方法:
...
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(onClickListener);
}
public OnClickListener onClickListener = new ...
答案 2 :(得分:0)
CalorieCounter活动中缺少button2.setOnClickListener
答案 3 :(得分:0)
如果OnclickListener有问题,可以使用此方法: 在xml活动中。
<Button
.......
android:onClick="functionBt1"
........
/>
在您的课程中,您应该创建一个名为“functionBt1”的方法
public void functionBt1(View v){
Intent i = new Intent(First.this,Second.class);
startActivity(i);
}
请记住将这两个活动放在AndroidManifest.xml中