Reputation: 691
My problem is when I change the text of a TextView
in my Main class, it returns nullPointerException.
here's my code:
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);
}
Then class that will update the status TextView is this:
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....
Pls help me. I cant find any solution in the net. I need to update the status....
heres my manifest:
...
<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>
...
In advance, thanks for any help.
Upvotes: 5
Views: 15201
Reputation: 7472
You see the view with id status
exists in the Main activity context, while you are trying to find it from the Contacts activity context. Thats why it is returning null
.
Try this, it should solve your problem :
Main Activity
public class Main extends Activity {
public static String sStatus = "";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display implementation of TabHost
....
}
@Override
protected void onResume() {
TextView labelView = (TextView) findViewById(R.id.status);
labelView.setText(status);
}
}
Contacts Activity
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){
Main.sStatus = status;
}
}
Upvotes: 4
Reputation: 23208
View is only associated with Main. You have to reset the contentView in Contacts. Modified code
public class Contacts extends ListActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//gather info to display in ListView
....
updateStatus("New Status");
}
private void updateStatus(String status){
TextView labelView = (TextView) findViewById(R.id.status);
labelView.setText(status);
}
}
Upvotes: 0