我在下面有一个布局文件(picture_section_fragment.xml),想将其加载到Java代码中,而不是将其静态地放置在主xml布局文件中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/pic_sec_frag_id">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="520dp"
    android:id="@+id/My_Container_1_ID"
    android:scaleType="centerCrop"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    </LinearLayout>


我很确定在mPager行下面的代码块中会发生错误(问题的底部)。我不想将picture_segment_fragment.xml文件静态地放置在主活动布局文件中的原因是,因为我在运行时通过URL加载图像(在单独的Java文件中,我没有将其放置在此处)。非常感谢您提供有关如何在Java代码中加载picture_segment_fragment.xml文件的帮助!顺便说一句,我没有将ViewPager放在主xml布局文件中,因为我不希望整个页面都可以滑动。我只希望页面的一部分可以滑动。

public class MainActivity extends AppCompatActivity {
// For this example, only two pages
static final int NUM_ITEMS = 3;
ViewPager mPager;
SlidePagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button clickButton = (Button) findViewById(R.id.getDirectionsButton);
    clickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Get Directions Pressed", Toast.LENGTH_LONG).show();
        }
    });

    /* Instantiate a ViewPager and a PagerAdapter. */
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new SlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setPageTransformer(true, new ZoomOutPageTransformer());
} ............................


错误:


  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {com.newmoney.night_life_now / com.newmoney.night_life_now.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v4。在空对象引用上的view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.access $ 900(ActivityThread.java:177)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler.java:102)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.os.Looper.loop(Looper.java:145)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5942)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于java.lang.reflect.Method.invoke(本机方法)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于java.lang.reflect.Method.invoke(Method.java:372)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于com.newmoney.night_life_now.MainActivity.onCreate(MainActivity.java:45)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.Activity.performCreate(Activity.java:6289)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.access $ 900(ActivityThread.java:177)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler.java:102)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.os.Looper.loop(Looper.java:145)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5942)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于java.lang.reflect.Method.invoke(本机方法)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于java.lang.reflect.Method.invoke(Method.java:372)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)
  12-07 23:31:31.729 23227-23227 /? E / AndroidRuntime:位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

最佳答案

如果这是您的片段的布局,则应将其放大并从onCreateView()返回。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
    return inflater.inflate(R.layout.picture_segment_fragment, container, false);
}

10-05 20:22