TextView崩溃?

时间:2014-07-28 18:11:10

标签: android crash textview

public class MainActivity extends Activity {


TextView tID;
TextView tName;
TextView tWorld;

protected void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.ps);


    tID = (TextView) findViewById(R.id.tID);
    tName = (TextView) findViewById(R.id.tName);
    tWorld = (TextView) findViewById(R.id.tWorld);
}

public void Search(View view) {

    int clist = -2;
    String oID = "";
    String oName = "";
    String oWorld = "";
    setContentView(R.layout.list);
    while (clist != -1)
    {
        oID = tID.getText().toString();
        tID.setText(oID+"ddf"+"\n");
        oName = tName.getText().toString();
        clist = -1;
    }

}
}

list.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/VScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

    <TextView
        android:id="@+id/tID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=" " />

    <TextView
        android:id="@+id/tName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=" " />

    <TextView
        android:id="@+id/tWorld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=" " />

</LinearLayout>

</ScrollView>

我知道这可能很简单,但在网上找不回答...... 为什么到达这里会崩溃?: 我知道这可能很简单,但在线查看并没有回答...... 为什么到达这里会崩溃?:

while (clist != -1)
    {
        oID = tID.getText().toString();
        tID.setText(oID+"ddf"+"\n");
        oName = tName.getText().toString();
        clist = -1;
    }

修改* Search()是ps.xml中的onClick按钮,而textview位于list.xml

1 个答案:

答案 0 :(得分:0)

onCreate()方法中:

setContentView(R.layout.ps);更改为setContentView(R.layout.list);,因为您使用list.xml作为主要布局。

并在setContentView(R.layout.list);方法中删除此行Search()

public void Search(View view) {

    int clist = -2;
    String oID = "";
    String oName = "";
    String oWorld = "";
   // Delete this line setContentView(R.layout.list);
    while (clist != -1)
    {
        oID = tID.getText().toString();
        tID.setText(oID+"ddf"+"\n");
        oName = tName.getText().toString();
        clist = -1;
    }

}