gpt4 book ai didi

java - 对话框中的Viewpager?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:47 26 4
gpt4 key购买 nike

我正在尝试创建一个对话框,您可以在其中单击“下一步”按钮以向右滑动到下一个屏幕。我正在使用 ViewPager 和适配器执行此操作:

  final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.voicedialog);
dialog.setCanceledOnTouchOutside(false);

MyPageAdapter adapter = new MyPageAdapter();
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(adapter);

但是,我收到一个 NullPointerException,指出 pager 为空。为什么会这样?这是页面适配器类:

public class MyPageAdapter extends PagerAdapter {
public Object instantiateItem(ViewGroup collection, int position) {

int resId = 0;
switch (position) {
case 0:
resId = R.id.voice1;
break;
case 1:
resId = R.id.voice2;
break;
}
return collection.findViewById(resId);
}

@Override
public int getCount() {
return 2;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}

这是我的 DIALOG 布局:

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

让我知道如何避免这种情况。

PS: view pager 中的每个布局都应该是这样的,只是不同而已。文本:

<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/voice2"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:text="Slide 1!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_gravity="center"
android:textSize="50sp" />

</RelativeLayout>

最佳答案

不使用枚举类

您应该在对话框中调用 findViewById。所以为此你必须在 findViewById 之前添加对话框..

像这样,

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

在解决了你的空指针异常之后,另一个问题的解决方案在这里,如果你不使用枚举类,你可以使用下面的代码......

主 Activity .java

package demo.com.pager;

import android.app.Dialog;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn= (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.voicedialog);
dialog.setCanceledOnTouchOutside(false);

MyPageAdapter adapter = new MyPageAdapter(MainActivity.this);
ViewPager pager = (ViewPager) dialog.findViewById(R.id.viewpager);
pager.setAdapter(adapter);
dialog.show();
}
});


}
}

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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="demo.com.pager.MainActivity">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

MyPageAdapter.java

package demo.com.pager;

import android.app.FragmentManager;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by rucha on 26/12/16.
*/

public class MyPageAdapter extends PagerAdapter {

Context mContext;
int resId = 0;

public MyPageAdapter(Context context) {
mContext = context;
}


public Object instantiateItem(ViewGroup collection, int position) {

/* int resId = 0;
switch (position) {
case 0:
resId = R.id.voice1;
break;
case 1:
resId = R.id.voice2;
break;
}

return collection.findViewById(resId);*/

LayoutInflater inflater = LayoutInflater.from(mContext);
switch (position) {
case 0:
resId = R.layout.fragment_blue;
break;
case 1:
resId = R.layout.fragment_pink;
break;
}
ViewGroup layout = (ViewGroup) inflater.inflate(resId, collection, false);
collection.addView(layout);
return layout;
}

@Override
public int getCount() {
return 2;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}

FragmentBlue.java

package demo.com.pager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.support.v4.app.Fragment;

public class FragmentBlue extends Fragment {

private static final String TAG = FragmentBlue.class.getSimpleName();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_blue, container, false);
return view;
}


}

fragment _blue.xml

<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:background="#4ECDC4">

</RelativeLayout>

语音对话.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

请检查并回复。

关于java - 对话框中的Viewpager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41326209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com