當我在onCreate方法之外聲明TextView時,我的應用程序停止,我這樣做是因爲我需要從其他方法訪問TextView變量。我會很感激任何幫助。在onCreate之外聲明變量 - Android
public class MainActivity extends ActionBarActivity {
TextView textView = (TextView)findViewById(R.id.defaultText);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
textView.setText("Hello");
}
}
只需將TextView定義爲一個類作用域變量(即將它放在任何方法範圍外的類的頂部) – Booger 2014-09-02 22:02:22
在Logcat中查找實際的錯誤消息當您提出這樣的問題時,請將Logcat – 2014-09-02 22:05:04
@Dale在閱讀代碼時答案很清楚 – TehCoder 2014-09-02 22:06:12