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);

、したがって、これらの変数はすべて null に設定されます。

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 に答える