How to set array elements to dynamic created textview in android
MainActivity1 :
public class MainActivity1 extends Activity implements OnClickListener {
EditText editText;
Button button;
public String Message;
public String[] mobileArray;
int len;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
mobileArray = editText.getText().toString().split(",");
len = mobileArray.length;
}
@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 void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout rl = (LinearLayout) findViewById(R.id.linear1);
TextView[] tv = new TextView[len];
for (int i = 0; i <=len; i++) {
tv[i] = new TextView(getBaseContext());
tv[i].setText("" +mobileArray[i] );
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#FF000000"));
rl.addView(tv[i]);
}
}
}
How to set array elements to dynamic created textview in android
Here everthing works fine, it creates dyanmic textview. But i need to display the array elements in textview.
When i try to set the array element app stops working. Please help me.
This my edit code
and my log cat is
12-26 02:13:44.897: E/AndroidRuntime(2779): FATAL EXCEPTION: main
12-26 02:13:44.897: E/AndroidRuntime(2779): Process: com.example.new_word, PID: 2779
12-26 02:13:44.897: E/AndroidRuntime(2779): java.lang.NullPointerException
12-26 02:13:44.897: E/AndroidRuntime(2779): at com.example.new_word.MainActivity1.onClick(MainActivity1.java:56)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.view.View.performClick(View.java:4438)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.view.View$PerformClick.run(View.java:18422)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.os.Handler.handleCallback(Handler.java:733)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.os.Looper.loop(Looper.java:136)
12-26 02:13:44.897: E/AndroidRuntime(2779): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-26 02:13:44.897: E/AndroidRuntime(2779): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:13:44.897: E/AndroidRuntime(2779): at java.lang.reflect.Method.invoke(Method.java:515)
12-26 02:13:44.897: E/AndroidRuntime(2779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-26 02:13:44.897: E/AndroidRuntime(2779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-26 02:13:44.897: E/AndroidRuntime(2779): at dalvik.system.NativeStart.main(Native Method)
12-26 02:13:47.467: I/Process(2779): Sending signal. PID: 2779 SIG: 9
Xml code is
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Button" />
</LinearLayout>
</ScrollView>