Android:将textview添加到linearlayout时出错

时间:2014-06-10 07:15:06

标签: android android-layout textview scrollview

以下是我的应用的OnCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection_screen);

        // this is automatically added by eclipse
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        log("Was here!");
        ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
        LinearLayout layout = (LinearLayout)findViewById(R.id.scrolllayout);
        List<ActivityManager.RunningAppProcessInfo> appsRunning = manager.getRunningAppProcesses();

        log("Now here!");
        for (ActivityManager.RunningAppProcessInfo v : appsRunning) {
            TextView TVinfo = new TextView(this);
            TVinfo.setText(v.processName);
            log("Looping here!");
            layout.addView(TVinfo, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            log("Looping here!");
        }

        appsRunning = null;   
        log("Finally here!");
        ScrollView scrollRunningApps = (ScrollView)findViewById(R.id.scrollView);
        scrollRunningApps.invalidate();
        scrollRunningApps.requestLayout();
    }

每当应用程序启动时执行上述方法,它总是在将第一个视图添加到线性布局对象后立即崩溃。所以这是日志输出:

06-10 01:13:23.172: I/TIMER(8319): Was here!
06-10 01:13:23.172: I/TIMER(8319): Now here!
06-10 01:13:23.172: I/TIMER(8319): Looping here!

这是我的xml文件,我遗漏了与之无关的部分:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/button"
    android:layout_alignLeft="@id/welcome_textView"
    android:layout_alignRight="@id/welcome_textView" >

    <LinearLayout
        android:id="@+id/scrolllayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

1。首先在ids.xml目录中创建一个文件/res,然后将以下XML架构添加到此文件中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type = "id" name = "mytextbox"></item>
</resources>

2。您的onCreate()方法应如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selection_screen);

    if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

3。将以下代码添加到PlaceholderFragment

@Override
public void onViewCreated(final View view, Bundle savedInstanceState){

    LinearLayout l = (LinearLayout) view.findViewById(R.id.scrolllayout);
    EditText e = new EditText(view.getContext());
    e.setId(R.id.mytextbox);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    e.setLayoutParams(lp);
    l.addView(e);

}

试试这个。它会起作用。

注意:我可以看到您尝试添加EditText中的Activity,也许您不希望从Fragment中添加Activity }。如果您仍希望通过Fragment进行此操作,请与我们联系。有办法实现,但在处理Fragment时,在成功创建{{1}}后添加/删除视图总是更安全。