for
ビューをレイアウトに追加するループを作成しようとしています。-loopfor
は正常に動作しています (初期化された変数で試しました) が、 から整数を取得する必要がありEditText
ます。私は a を使用しtry-catch
、LogCat は次のことを教えてくれます...
java.lang.NumberFormatException: invalid int "".
ウェブサイト developers.android.com は、これは文字列を整数に誤って変換したことによるものだと言っていますが、EditText
.
これは私のコードです...
public class UserPref2Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = Integer.parseInt(change);
int j; //iterates through the for loop
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(j=1;j<i;j++)
{
EditText name = new EditText(this);
name.setText("Name:");
EditText type = new EditText(this);
type.setText("Type:");
EditText bits = new EditText(this);
bits.setText("Bits:");
ll.addView(name);
ll.addView(type);
ll.addView(bits);
}
this.setContentView(sv);
}
catch (Exception e)
{
//sends actual error message to the log
Log.e("ERROR", "ERROR IN CODE:" + e.toString());
//prints out location of error
e.printStackTrace();
}
}
}
そして、これは私のXMLファイルです...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/userLayout" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter number of sensors:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/num_sensors" />