当我运行我的应用程序时,我得到一个空白屏幕。我有2个片段及其xml文件。
以下是我的全部代码:
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="autogenie.designtrial.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/view"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/list" />
</RelativeLayout>
Mainactivity.java
package autogenie.designtrial;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager v=(ViewPager)findViewById(R.id.view);
List<Fragment> fragments=new ArrayList<>();
fragments.add(Fragment.instantiate(this, frag1.class.getName()));
fragments.add(Fragment.instantiate(this, frag2.class.getName()));
fragpageadapter fragpageadapter0=new fragpageadapter(getSupportFragmentManager(),fragments);
v.setAdapter(fragpageadapter0);
}
}
frag1.java
package autogenie.designtrial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by sadhana on 25/1/16.
*/
public class frag1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate and locate the main ImageView
final View v1 = inflater.inflate(R.layout.frag1, container, false);
//...
return v1;
}
}
frag1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:layout_gravity="center" />
</LinearLayout>
frag2.java
package autogenie.designtrial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by sadhana on 25/1/16.
*/
public class frag2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate and locate the main ImageView
final View v3 = inflater.inflate(R.layout.frag2, container, false);
//...
return v3;
}
}
frag2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar" />
</LinearLayout>
fragpageadapter.java
package autogenie.designtrial;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class fragpageadapter extends FragmentPagerAdapter {
List<Fragment> f;
public fragpageadapter(FragmentManager fm,List<Fragment> f) {
super(fm);
this.f=f;
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = f.get(position);
break;
case 1:
fragment = f.get(position);
break;}
return fragment;
}
@Override
public int getCount() {
return 0;
}
}
答案 0 :(得分:3)
您在getCount()
fragpageadapter
中返回0
@Override
public int getCount() {
return 0;
}
将其更改为f.size()
,即list.size
@Override
public int getCount() {
return f.size;
}
答案 1 :(得分:1)
验证您的方法getCount()
。它需要返回碎片的数量。
例如:
@Override
public int getCount() {
return f.size();
}