我在启动意图时遇到此错误 ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,Intent)行: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()函数时会触发错误,但我不知道如何修复它
答案 0 :(得分:1)
您正在设置变量lblName和所有这些变量,以包含所有子视图...但您在类级别执行此操作。直到布局膨胀时,才会在读取的行中创建这些视图
setContentView(R.layout.single_list_item);
,所有这些变量都设置为null。
在setContentView()调用之后设置变量并查看它是如何运行的。
答案 1 :(得分: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()调用之前初始化视图。这就是为什么你会遇到这个问题。
答案 2 :(得分: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);