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.
You see the view with id
statusexists in the Main activity context, while you are trying to find it from the Contacts activity context. Thats why it is returningnull.Try this, it should solve your problem :
Main Activity
Contacts Activity