我的問題是當我在Main.class中更改TextView
的文本時,它返回nullPointerException。如何從其他Activity類訪問Activity的視圖
這裏是我的代碼:
main.xml中
<TabHost ...>
<RelativeLayout ...>
<LinearLayout ...>
<TabWidget .../>
<FrameLayout ...>
<ListView ../>
</FrameLayout>
</LinearLayout>
<LinearLayout ...>
<TextView android:id="@+id/status" ... />
</LinearLayout>
</RelativeLayout>
</TabHost>
Main.class
....
TabHost tabHost;
TabHost.TabSpec spec;
Intent intent;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display implementation of TabHost
....
//Contacts tab
intent = new Intent().setClass(this, Contacts.class);
spec = tabHost.newTabSpec("contacts").setIndicator("Contacts", res.getDrawable(R.drawable.ic_tab_contacts)).setContent(intent);
tabHost.addTab(spec);
}
然後類,將更新狀態的TextView是這樣的:
Contacts.class
public class Contacts extends ListActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//gather info to display in ListView
....
updateStatus("New Status");
}
private void updateStatus(String status){
TextView labelView = (TextView) findViewById(R.id.status);
labelView.setText(status);
}
}
labelView.setText(status); Error occurs here....
請幫幫我。我無法找到網絡中的任何解決方案。 我需要更新狀態....
繼承人我的清單:
...
<application ...>
<activity android:name=".Main" ... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Contacts" android:label="@string/app_name"></activity>
<activity android:name=".OtherClass" android:label="@string/app_name"></activity>
<activity android:name=".OtherClass2" android:label="@string/app_name"></activity>
</application>
...
在前進,感謝您的幫助。
Aki先生,請給我執行主類的sStatus ...因爲我也試過這個解決方案......但是給我一個錯誤.. – jayellos
請不要打電話給我先生。你是什麼意思的實施?你遇到了什麼錯誤? –
我宣佈公共TextView sStatus;但不能找到Main.sStatus。 – jayellos