Sunday, May 19, 2013

[android help] Android will crash depending on layout positioning


Here's the layout that works :



xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip" >


android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:context=".MainActivity" >





and here my Activity OnCreate method:



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pager = (ViewPager) findViewById(R.id.pager);

adapter = new MyPagerAdapter(getSupportFragmentManager());

pager.setAdapter(adapter);

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
tabs.setDividerColor(Color.BLUE);
tabs.setBackgroundColor(Color.DKGRAY);
tabs.setTextColor(Color.WHITE);
tabs.setIndicatorColor(Color.GREEN);

}


If in the layout I inverse the PagerSlidingTabStrip and the ViewPager, the project will crash at startup, while in the first implementation, it will work fine.


Here's the error :



05-19 16:50:11.636: E/AndroidRuntime(305): FATAL EXCEPTION: main
05-19 16:50:11.636: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hsware.peeem/com.hsware.peeem.MainActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-19 16:50:11.636: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
05-19 16:50:11.636: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-19 16:50:11.636: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-19 16:50:11.636: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305): Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305): at com.hsware.peeem.MainActivity.onCreate(MainActivity.java:41)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)


It appears that it can't retrieve de R.id.pager, I'm not sure.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...