[英]Unfortunately the app has stopped working - android
单击按钮时会发生这种情况
我的Activity.XML文件
<LinearLayout 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">
<EditText android:id="@+id/enter_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/enter_number"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_calculate"
android:onClick="calculate"
/>
</LinearLayout>
.java文件
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class Tables extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
int number=0;
int i=1;
int next=0;
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
number=Integer.parseInt(message);
TextView textView = new TextView(this);
String[] myString = new String[10]; //create array
for (i = 1; i <= myString.length; i++) {
next= number + next;
myString[i] = String.valueOf(number)+'x'+String.valueOf(i)+'='+String.valueOf(next)+'\n';
// textView.setText(myString[i]);
// setContentView(textView);
}
StringBuilder builder = new StringBuilder();
for (String s : myString){
builder.append(s+" ");
textView.setText(builder.toString());
}
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tables, menu);
return true;
}
}
我认为错误是在显示期间出现的。 但是找不到解决方案。
谢谢,
您必须在活动中定义一个calculate
方法。 在布局文件中设置onClick
属性后,单击按钮时,Android Framework将尝试调用Tables活动的follow方法:
public void calculate(View v){
//put you code here this will be execute when the button is clicked
}
布局文件看起来像是用于您的主要活动,而不是用于Tables
活动。 确保您的MainActivity
类具有带有签名public void calculate(View view)
以便活动可以响应按钮的单击。 这很可能是问题所在,而不是显示Tables
活动的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.