I am trying to implement a swipe view without tabs, but the app crashes.
Here is the code:
MainActivity.java
package com.application;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity
{
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager)findViewById(R.id.view_pager);
viewPager.setAdapter(pagerAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PagerAdapter.java
package com.application;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter
{
public PagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
}
@Override
public Fragment getItem(int arg0) {
switch(arg0)
{
case 0:
return new AndroidFragment();
case 1:
return new IosFragment();
case 2:
return new WindowsFragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
AndroidFragment.java
package com.application;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class AndroidFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_android, container);
((TextView)view.findViewById(R.id.text_view)).setText("Android");
return view;
}
}
fragment_android.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center" >
<TextView android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.application"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
IosFragment and WindowsFragment are just like AndroidFragment.
And it gives me the error:
Choreographer.doCallbacks(int, long) line: 558
Choreographer.doFrame(long, int) line: 525
Choreographer$FrameDisplayEventReceiver.run() line: 711
Handler.handleCallback(Message) line: 615
Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4921
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 1038
ZygoteInit.main(String[]) line: 805
NativeStart.main(String[]) line: not available [native method]
No comments:
Post a Comment