使用for循环生成TextView不起作用?

时间:2015-04-20 16:09:36

标签: java android arrays for-loop textview

String[] channels = {"a", "b", "c"}

    for (int i = 0; i < channels.length; i++) {
        TextView iteratorView = new TextView(TheActivity.this);
        iteratorView.setText(channels[i]);
        myLinearLayout.addView(iteratorView);
    }

我真的认为它会起作用,但应用程序崩溃了?我做错了什么?

LogCat文件:

04-20 18:11:48.105: D/ViewRootImpl(9584): ViewPostImeInputStage ACTION_DOWN
04-20 18:11:48.265: I/Timeline(9584): Timeline: Activity_launch_request id:za.hseldo.client time:229926093
04-20 18:11:48.365: D/AndroidRuntime(9584): Shutting down VM
04-20 18:11:48.375: I/art(9584): Background partial concurrent mark sweep GC freed 254(19KB) AllocSpace objects, 1(144KB) LOS objects, 14% free, 95MB/111MB, paused 10.207ms total 18.633ms
04-20 18:11:48.385: E/AndroidRuntime(9584): FATAL EXCEPTION: main
04-20 18:11:48.385: E/AndroidRuntime(9584): Process: za.hseldo.client, PID: 9584
04-20 18:11:48.385: E/AndroidRuntime(9584): java.lang.RuntimeException: Unable to start activity ComponentInfo{za.hseldo.client/za.hseldo.client.Aansteekbord}: java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 1:
04-20 18:11:48.385: E/AndroidRuntime(9584): [
04-20 18:11:48.385: E/AndroidRuntime(9584):  ^
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2694)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2759)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread.access$900(ActivityThread.java:178)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.os.Looper.loop(Looper.java:145)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread.main(ActivityThread.java:5944)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.lang.reflect.Method.invoke(Native Method)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.lang.reflect.Method.invoke(Method.java:372)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
04-20 18:11:48.385: E/AndroidRuntime(9584): Caused by: java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 1:
04-20 18:11:48.385: E/AndroidRuntime(9584): [
04-20 18:11:48.385: E/AndroidRuntime(9584):  ^
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.util.regex.Pattern.compileImpl(Native Method)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.util.regex.Pattern.compile(Pattern.java:411)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.util.regex.Pattern.<init>(Pattern.java:394)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.util.regex.Pattern.compile(Pattern.java:381)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at java.lang.String.replaceAll(String.java:1600)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at za.hseldo.client.Aansteekbord.onCreate(Aansteekbord.java:39)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.Activity.performCreate(Activity.java:6289)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
04-20 18:11:48.385: E/AndroidRuntime(9584):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
04-20 18:11:48.385: E/AndroidRuntime(9584):     ... 10 more
04-20 18:12:10.835: I/Process(9584): Sending signal. PID: 9584 SIG: 9

2 个答案:

答案 0 :(得分:1)

我让这个工作。你能发布剩下的代码吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout layout = new      LinearLayout(this.getApplicationContext());
    String[] channels = {"a", "b", "c"};

    for (int i = 0; i < channels.length; i++) {
        TextView iteratorView = new TextView(this);
        iteratorView.setText(channels[i]);
        layout.addView(iteratorView);
    }
setContentView(layout);
}

答案 1 :(得分:1)

你没有给#34 ;;&#34;在String[] channels = {"a", "b", "c"}之后

写下来:String[] channels = {"a", "b", "c"};