这发生在按钮单击
我的 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;
}
}
我认为错误是在显示期间。但是找不到解决办法。
谢谢,