1

我收到这个错误ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585当我开始一个意图

public class SingleMenuItemActivity  extends Activity {

TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;

String name,yesterday,today,high,low,change,pcchange;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     setContentView(R.layout.single_list_item);



      lblName = (TextView) findViewById(R.id.name);
         lblyest = (TextView) findViewById(R.id.yesterday);
         lbltoday = (TextView) findViewById(R.id.today);
         lblHigh = (TextView) findViewById(R.id.high);
         lblLow = (TextView) findViewById(R.id.low);
         lblChange = (TextView) findViewById(R.id.change);
         lblPcchange = (TextView) findViewById(R.id.pcchange);  

         Intent in = getIntent();
         name = in.getStringExtra("name");;
         yesterday = in.getStringExtra("yesterday");
         today =in.getStringExtra("today");
         high =in.getStringExtra("high");
         low = in.getStringExtra("low");
         change = in.getStringExtra("change");
         pcchange =in.getStringExtra("pcchange");


      //Error doesnt happen when the lines below are commented
        lblName.setText((CharSequence)name.toString());
        lblyest.setText(yesterday.toString());
        lbltoday.setText(today.toString());
        lblHigh.setText(high.toString());
        lblLow.setText(low.toString());
        lblChange.setText(change.toString());
        lblPcchange.setText(pcchange.toString());  

}

}

我知道当我执行 settext() 函数时会触发错误,但我不知道如何修复它

4

3 回答 3

1

您正在设置变量 lblName 和所有这些变量,以包含所有子视图的......但您是在类级别进行的。在布局膨胀之前不会创建这些视图,在读取的行中 setContentView(R.layout.single_list_item);

,所以所有这些变量都设置为空。

在 setContentView() 调用之后设置变量,看看它是如何进行的。

于 2012-10-09T09:51:31.547 回答
0

试试这个代码。

public class SingleMenuItemActivity  extends Activity {

    TextView lblName;
    TextView lblyest;
    TextView lbltoday;
    TextView lblHigh;
    TextView lblLow;
    TextView lblChange;
    TextView lblPcchange;

    String name, yesterday, today, high, low, change, pcchange;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate( savedInstanceState );
        setContentView( R.layout.single_list_item );
        Intent in = getIntent();

        name = in.getStringExtra( "name" );
        ;
        yesterday = in.getStringExtra( "yesterday" );
        today = in.getStringExtra( "today" );
        high = in.getStringExtra( "high" );
        low = in.getStringExtra( "low" );
        change = in.getStringExtra( "change" );
        pcchange = in.getStringExtra( "pcchange" );

        lblName = (TextView) findViewById( R.id.name );
        lblyest = (TextView) findViewById( R.id.yesterday );
        lbltoday = (TextView) findViewById( R.id.today );
        lblHigh = (TextView) findViewById( R.id.high );
        lblLow = (TextView) findViewById( R.id.low );
        lblChange = (TextView) findViewById( R.id.change );
        lblPcchange = (TextView) findViewById( R.id.pcchange );

        lblName.setText( (CharSequence) name.toString() );
      /*  lblyest.setText(yesterday.toString());
        lbltoday.setText(today.toString());
        lblHigh.setText(high.toString());
        lblLow.setText(low.toString());
        lblChange.setText(change.toString());
        lblPcchange.setText(pcchange.toString());  */

    }


}

实际上,您不能在 setContentView() 调用之前初始化视图。这就是为什么,你得到这个问题。

于 2012-10-09T09:49:34.143 回答
0

将这些行移到后面setContentView(R.layout.single_list_item);

TextView lblName = (TextView) findViewById(R.id.name);
TextView lblyest = (TextView) findViewById(R.id.yesterday);
TextView lbltoday = (TextView) findViewById(R.id.today);
TextView lblHigh = (TextView) findViewById(R.id.high);
TextView lblLow = (TextView) findViewById(R.id.low);
TextView lblChange = (TextView) findViewById(R.id.change);
TextView lblPcchange = (TextView) findViewById(R.id.pcchange);
于 2012-10-09T09:50:04.273 回答