Android 该项目赢得了';t运行,但没有错误
我搜索了我的问题,发现其他人的问题非常相似,但他们的解决方案不一样。我编写了一个代码,根据用户点击的按钮执行简单的乘法任务。main.xml文件和附带的java文件都是无错误的,页面上甚至没有警告。一切看起来都很好,但当我尝试运行程序时,它会弹出提示,告诉我有错误,请修复它们。控制台和LogCat中均未显示任何内容。当我转到windows->show view->problems时,它也不会列出与该程序相关的任何内容 我的main.xml代码是:Android 该项目赢得了';t运行,但没有错误,android,eclipse,Android,Eclipse,我搜索了我的问题,发现其他人的问题非常相似,但他们的解决方案不一样。我编写了一个代码,根据用户点击的按钮执行简单的乘法任务。main.xml文件和附带的java文件都是无错误的,页面上甚至没有警告。一切看起来都很好,但当我尝试运行程序时,它会弹出提示,告诉我有错误,请修复它们。控制台和LogCat中均未显示任何内容。当我转到windows->show view->problems时,它也不会列出与该程序相关的任何内容 我的main.xml代码是: <RelativeLayout xmlns
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="@string/number" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="16dp"
android:layout_marginTop="23dp"
android:text="@string/1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="@string/2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignRight="@+id/textView1"
android:text="@string/3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:text="@string/4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:text="@string/5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_alignLeft="@+id/button3"
android:text="@string/6" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginTop="22dp"
android:text="@string/7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignLeft="@+id/button5"
android:text="@string/8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_alignLeft="@+id/button6"
android:text="@string/9" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_below="@+id/button8"
android:layout_marginTop="70dp"
/>
</RelativeLayout>
抽象类
?为什么?您将永远无法实例化抽象的活动
删除
摘要
声明,它将正常工作。并确保您已在应用程序的清单中声明了Main
acivity。我不会检查所有乘法逻辑,但您问题的核心是为什么按钮单击不起作用。您还需要做两件事:
//NOTE Main no longer implements View.OnClickListener
public class Main extends Activity {
// creates a button
private Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//assigns the resource id of 1 - 9 to each button.
bone = (Button) findViewById(R.id.button1);
btwo = (Button) findViewById(R.id.button2);
bthree = (Button) findViewById(R.id.button3);
bfour = (Button) findViewById(R.id.button4);
bfive = (Button) findViewById(R.id.button5);
bsix = (Button) findViewById(R.id.button6);
bseven = (Button) findViewById(R.id.button7);
beight = (Button) findViewById(R.id.button8);
bnine = (Button) findViewById(R.id.button9);
//NOTE seperate onClickListener()
OnClickListener oc = new OnClickListener(){
@Override
//creates a method (or action) for when the button is clicked.
public void onClick(View view) //NOTE the capital C in Click
{
//Makes a variable for the entered number
Double amount = 0.0;
Double product = 0.0;
Double variable = 0.0;
// constants
final double one = 1;
final double two = 2;
final double three = 3;
final double four = 4;
final double five = 5;
final double six = 6;
final double seven = 7;
final double eight = 8;
final double nine = 9;
if (view.getId() == R.id.button1)
{
variable = one;
}
if (view.getId() == R.id.button2)
{
variable = two;
}
if (view.getId()== R.id.button3)
{
variable = three;
}
if (view.getId() == R.id.button4)
{
variable = four;
}
if (view.getId() == R.id.button5)
{
variable = five;
}
if (view.getId()== R.id.button6)
{
variable = six;
}
if (view.getId() == R.id.button7)
{
variable = seven;
}
if (view.getId() == R.id.button8)
{
variable = eight;
}
if (view.getId()== R.id.button9)
{
variable = nine;
}
}
};
//NOTE setting the seperate OnClickListener
//Adds the buttons to the onclicklistener
bone.setOnClickListener(oc);
btwo.setOnClickListener(oc);
bthree.setOnClickListener(oc);
bfour.setOnClickListener(oc);
bfive.setOnClickListener(oc);
bsix.setOnClickListener(oc);
bseven.setOnClickListener(oc);
beight.setOnClickListener(oc);
bnine.setOnClickListener(oc);
//...snip...
}
}
您是否在AndroidManifest.xml文件中声明了该活动?如果没有,先试试。否则,请在尝试运行程序时发布logcat输出。您应该始终发布logcat。如果您使用eclipse,请查看问题视图。这里描述了哪些问题(窗口->显示视图->问题)?控制台视图中有输出吗?你试过清理这个项目(项目->清理…)吗?不过,我发了一篇关于我的日志的帖子。我发布了日志,日志仍然是空白的。当我尝试运行该程序时,它中根本没有显示任何内容。与此程序相关的任何内容都不在问题视图中,但没有。我没有尝试清理该项目,我现在就这样做。我想我声明了活动,但我会再次检查。我尝试删除抽象,但它告诉我“类型Main必须实现继承的抽象方法视图。OnClickListener(视图)”您是否将主活动定义为启动程序活动?你必须有一些反馈!是:@LucianCat如果将该类声明为implementing View.OnClickListener,则需要提供该接口定义的方法。严格地说,这对于活动来说是可选的,但是如果你想对点击做出反应,这是实现它的一种方法。@yugi我知道,我应该有一些东西,但这正是让我难堪的。我右键点击我的程序,点击运行android应用程序,弹出一个错误,然后什么也没有。我尝试过多次,但控制台和logcat中都没有显示任何内容,Problems视图中的任何内容都与我过去创建的其他程序有关。
public abstract class Main extends Activity implements View.OnClickListener{
...
}
//NOTE Main no longer implements View.OnClickListener
public class Main extends Activity {
// creates a button
private Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine;
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//assigns the resource id of 1 - 9 to each button.
bone = (Button) findViewById(R.id.button1);
btwo = (Button) findViewById(R.id.button2);
bthree = (Button) findViewById(R.id.button3);
bfour = (Button) findViewById(R.id.button4);
bfive = (Button) findViewById(R.id.button5);
bsix = (Button) findViewById(R.id.button6);
bseven = (Button) findViewById(R.id.button7);
beight = (Button) findViewById(R.id.button8);
bnine = (Button) findViewById(R.id.button9);
//NOTE seperate onClickListener()
OnClickListener oc = new OnClickListener(){
@Override
//creates a method (or action) for when the button is clicked.
public void onClick(View view) //NOTE the capital C in Click
{
//Makes a variable for the entered number
Double amount = 0.0;
Double product = 0.0;
Double variable = 0.0;
// constants
final double one = 1;
final double two = 2;
final double three = 3;
final double four = 4;
final double five = 5;
final double six = 6;
final double seven = 7;
final double eight = 8;
final double nine = 9;
if (view.getId() == R.id.button1)
{
variable = one;
}
if (view.getId() == R.id.button2)
{
variable = two;
}
if (view.getId()== R.id.button3)
{
variable = three;
}
if (view.getId() == R.id.button4)
{
variable = four;
}
if (view.getId() == R.id.button5)
{
variable = five;
}
if (view.getId()== R.id.button6)
{
variable = six;
}
if (view.getId() == R.id.button7)
{
variable = seven;
}
if (view.getId() == R.id.button8)
{
variable = eight;
}
if (view.getId()== R.id.button9)
{
variable = nine;
}
}
};
//NOTE setting the seperate OnClickListener
//Adds the buttons to the onclicklistener
bone.setOnClickListener(oc);
btwo.setOnClickListener(oc);
bthree.setOnClickListener(oc);
bfour.setOnClickListener(oc);
bfive.setOnClickListener(oc);
bsix.setOnClickListener(oc);
bseven.setOnClickListener(oc);
beight.setOnClickListener(oc);
bnine.setOnClickListener(oc);
//...snip...
}
}