Wednesday, February 26, 2014

When the help novice developers tabhost


Layout file:

xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent">

android: layout_height = "fill_parent"
android: id = "@ + id / thid">


android: layout_height = "match_parent"
android: id = "@ + id/tv1"
android: text = "text1" />
android: layout_height = "match_parent"
android: id = "@ + id/tv2"
android: text = "text2" />
android: layout_height = "match_parent"
android: id = "@ + id/tv3"
android: text = "text3" />


activity:
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_tabspace);

tabHost = (TabHost) this.findViewById (R.id.thid);



ts1 = tabHost.newTabSpec ("tab1");
ts2 = tabHost.newTabSpec ("tab2");
ts3 = tabHost.newTabSpec ("tab3");

When ts1.setContent (R.id.tv1) ;/ / this line of code to perform this error
ts2.setContent (R.id.tv2);
ts3.setContent (R.id.tv3);

tabHost.addTab (ts1);
tabHost.addTab (ts2);
tabHost.addTab (ts3);
}

Error message:

11-01 12:26:28.085: E / AndroidRuntime (299): FATAL EXCEPTION: main
11-01 12:26:28.085: E / AndroidRuntime (299): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.flp.control / com.flp.control.TabspaceActivity}: java.lang.NullPointerException
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2663)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2679)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread.access $ 2300 (ActivityThread.java: 125)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 2033)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.os.Handler.dispatchMessage (Handler.java: 99)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.os.Looper.loop (Looper.java: 123)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread.main (ActivityThread.java: 4627)
11-01 12:26:28.085: E / AndroidRuntime (299): at java.lang.reflect.Method.invokeNative (Native Method)
11-01 12:26:28.085: E / AndroidRuntime (299): at java.lang.reflect.Method.invoke (Method.java: 521)
11-01 12:26:28.085: E / AndroidRuntime (299): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 868)
11-01 12:26:28.085: E / AndroidRuntime (299): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 626)
11-01 12:26:28.085: E / AndroidRuntime (299): at dalvik.system.NativeStart.main (Native Method)
11-01 12:26:28.085: E / AndroidRuntime (299): Caused by: java.lang.NullPointerException
11-01 12:26:28.085: E / AndroidRuntime (299): at android.widget.TabHost $ ViewIdContentStrategy (TabHost.java: 586).
11-01 12:26:28.085: E / AndroidRuntime (299): at android.widget.TabHost $ ViewIdContentStrategy (TabHost.java: 581).
11-01 12:26:28.085: E / AndroidRuntime (299): at android.widget.TabHost $ TabSpec.setContent (TabHost.java: 438)
11-01 12:26:28.085: E / AndroidRuntime (299): at com.flp.control.TabspaceActivity.onCreate (TabspaceActivity.java: 27)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java: 1047)
11-01 12:26:28.085: E / AndroidRuntime (299): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2627)
11-01 12:26:28.085: E / AndroidRuntime (299): ... 11 more
11-01 12:30:22.085: E / AndroidRuntime (323): FATAL EXCEPTION: main
11-01 12:30:22.085: E / AndroidRuntime (323): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.flp.control / com.flp.control.TabspaceActivity}: java.lang.NullPointerException
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2663)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2679)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread.access $ 2300 (ActivityThread.java: 125)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 2033)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.os.Handler.dispatchMessage (Handler.java: 99)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.os.Looper.loop (Looper.java: 123)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread.main (ActivityThread.java: 4627)
11-01 12:30:22.085: E / AndroidRuntime (323): at java.lang.reflect.Method.invokeNative (Native Method)
11-01 12:30:22.085: E / AndroidRuntime (323): at java.lang.reflect.Method.invoke (Method.java: 521)
11-01 12:30:22.085: E / AndroidRuntime (323): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 868)
11-01 12:30:22.085: E / AndroidRuntime (323): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 626)
11-01 12:30:22.085: E / AndroidRuntime (323): at dalvik.system.NativeStart.main (Native Method)
11-01 12:30:22.085: E / AndroidRuntime (323): Caused by: java.lang.NullPointerException
11-01 12:30:22.085: E / AndroidRuntime (323): at android.widget.TabHost $ ViewIdContentStrategy (TabHost.java: 586).
11-01 12:30:22.085: E / AndroidRuntime (323): at android.widget.TabHost $ ViewIdContentStrategy (TabHost.java: 581).
11-01 12:30:22.085: E / AndroidRuntime (323): at android.widget.TabHost $ TabSpec.setContent (TabHost.java: 438)
11-01 12:30:22.085: E / AndroidRuntime (323): at com.flp.control.TabspaceActivity.onCreate (TabspaceActivity.java: 27)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java: 1047)
11-01 12:30:22.085: E / AndroidRuntime (323): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2627)
11-01 12:30:22.085: E / AndroidRuntime (323): ... 11 more



I am surprised that how it will be short-selling pointer, I use debug saw, R.id.tv1 there is value, ts1 is not empty, do not know how this is going, ask teachers under the guidance of
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
android Tabhost parts
TabHost the xml and initialization has certain requirements, specific sdk and the landlord can look at this link.
Code to help you simply changed a bit, not reported null pointer exception, the landlord according to their needs and then changed it.
 xmlns: tools = "http://schemas.android.com/tools" 
android: layout_width = "match_parent"
android: layout_height = "match_parent">

android: id = "@ + id / thid"
android: layout_width = "match_parent"
android: layout_height = "match_parent">

android: id = "@ android: id / tabs"
android: layout_width = "match_parent"
android: layout_height = "wrap_content">


android: id = "@ android: id / tabcontent"
android: layout_width = "match_parent"
android: layout_height = "match_parent">

android: id = "@ + id/tv1"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: text = "text1" />

android: id = "@ + id/tv2"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: text = "text2" />

android: id = "@ + id/tv3"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: text = "text3" />





 public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_tabspace);

TabHost tabHost = (TabHost) this.findViewById (R.id.thid);
tabHost.setup ();

TabSpec ts1 = tabHost.newTabSpec ("tab1");
TabSpec ts2 = tabHost.newTabSpec ("tab2");
TabSpec ts3 = tabHost.newTabSpec ("tab3");

ts1.setIndicator ("tab1");
ts2.setIndicator ("tab2");
ts3.setIndicator ("tab3");

When ts1.setContent (R.id.tv1) ;/ / this line of code to perform this error
ts2.setContent (R.id.tv2);
ts3.setContent (R.id.tv3);

tabHost.addTab (ts1);
tabHost.addTab (ts2);
tabHost.addTab (ts3);
}

No comments:

Post a Comment