[英]Unfortunately the app has stopped working
Please tell me why my app is not working I have made a coffee calculator app.请告诉我为什么我的应用程序不起作用我制作了一个咖啡计算器应用程序。 Here is the xml code这是xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/disp"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="Add"
android:text="@string/add"
android:textColor="@android:color/black"
android:textSize="30sp"
android:textStyle="bold"
android:background="#E57373" />
<TextView
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/text"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="Sub"
android:text="@string/sub"
android:textColor="@android:color/black"
android:textSize="30sp"
android:textStyle="bold"
android:background="#E57373" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/price"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/total"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/res"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="210sp"
android:onClick="Order"
android:text="@string/order"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
android:textAllCaps="true"
android:background="#E57373"/>
</LinearLayout>
And this is my Java code这是我的 Java 代码
package com.example.extra;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
public int num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Add(View view){
//On click method for Add button
TextView text=(TextView)findViewById(R.id.sum);
num=num+1;
text.setText(num);
}
public void Sub(View view){
//On click method for Sub button
TextView text=(TextView)findViewById(R.id.sum);
if(num==0)
text.setText(0);
else
{ num=num-1;
text.setText(num);
}
}
public void Order(View view){
//On click method for button ORDER
TextView text=(TextView)findViewById(R.id.res);
text.setText("$" + num*10);
}
}
When I am launching the app and clicking the add button or sub button then the app crashes.But the ORDER button is working fine.Please help...当我启动应用程序并单击添加按钮或子按钮时,应用程序崩溃。但订单按钮工作正常。请帮助...
The reason your app is crashing is because of the int you are putting in your TextViews.您的应用程序崩溃的原因是因为您在 TextViews 中放入了 int。 setText()
takes a string variable or an int specifying a resource Id. setText()
接受一个字符串变量或一个指定资源 ID 的 int。 In this case the int is not specifying a resource ID so it is failing.在这种情况下, int 没有指定资源 ID,因此它失败了。 Change the setText()
in Sub
and Add
to this:更改Sub
的setText()
并Add
到此:
text.setText("" + num);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.