Java Android Studio PageViewer findViewById(R.Id.FragmentIteam)返回空值
我得到了一个包含3个片段的页面查看器(主页、注册、登录)。我想要登录片段的EditText中的文本。如果调用findViewById,则每次都返回null。那我错了什么? MyPageViewer.java:Java Android Studio PageViewer findViewById(R.Id.FragmentIteam)返回空值,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我得到了一个包含3个片段的页面查看器(主页、注册、登录)。我想要登录片段的EditText中的文本。如果调用findViewById,则每次都返回null。那我错了什么? MyPageViewer.java: public class MyViewPager extends ViewPager { public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs);
public class MyViewPager extends ViewPager {
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
initMyScroller();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
private void initMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyScroller extends Scroller {
public MyScroller(Context context) {
//super(context, new LinearInterpolator()); // my LinearInterpolator
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, duration);
}
}
}
fragment_login.xml:
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="280dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<EditText
android:id="@+id/edt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username / Email"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:drawableLeft="@drawable/ic_user"
android:drawablePadding="15dp"/>
<EditText
android:id="@+id/edt_pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Passwort"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_lock"
android:drawablePadding="15dp"/>
<com.ornach.nobobutton.NoboButton
android:id="@+id/btn_login"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:padding="10dp"
android:onClick="GoToMenu"
app:nb_backgroundColor="@android:color/transparent"
app:nb_borderColor="#666"
app:nb_borderWidth="1dp"
app:nb_focusColor="#DDD"
app:nb_text="ANMELDEN"
app:nb_textSize="14sp" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private final int TOTAL_PAGE=3;
private int sHeight;
private int sWidth;
View navBackground, navBottom;
View navHome, navLogin, navRegister;
EditText edt_pw, edt_login;
ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash)
{
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 32)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startup);
SharedPreferences pref = this.getSharedPreferences("SystemSettings", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
if ((pref.getInt("avi", 0)) == 0) {
editor.putInt("avi", 1);
editor.commit();
Intent intent = new Intent(this, instruction.class);
startActivity(intent);
}
screenMeasurement();
navBackground = findViewById(R.id.img_nav_bg);
navBottom = findViewById(R.id.navbottom);
navHome = findViewById(R.id.nav_home);
navLogin = findViewById(R.id.nav_login);
navRegister = findViewById(R.id.nav_register);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
mSectionsPagerAdapter.getItem(2);
navBackground.post(new Runnable() {
@Override
public void run() {
int w = (int) (sWidth*2.5);
int h = w/2;
FrameLayout.LayoutParams bgParams = (FrameLayout.LayoutParams) navBackground.getLayoutParams();
bgParams.width = w;
bgParams.height = h;
//bgParams.leftMargin = -1*((w/2)-(sWidth/2));
bgParams.bottomMargin = (int) (-1*(h/2.5));
navBackground.requestLayout();
int left = (sWidth/2) - (w/2);
navBackground.setX(left);
resizeNavIcon(navHome,w);
resizeNavIcon(navLogin,w);
resizeNavIcon(navRegister,w);
}
});
}
private void resizeNavIcon(View view, int bgWidth){
int iconWidth = (bgWidth*43)/500;
int m = iconWidth/15;
LinearLayout.LayoutParams btnParams = (LinearLayout.LayoutParams) view.getLayoutParams();
btnParams.width = iconWidth;
btnParams.height = iconWidth;
btnParams.leftMargin = (int) (m*1.5);
btnParams.rightMargin = (int) (m*1.5);
btnParams.topMargin = m;
btnParams.bottomMargin = m;
view.setLayoutParams(btnParams);
view.requestLayout();
int p = (int) (iconWidth/3.5);
//Log.e("TAG","width: "+iconWidth+" Padding: "+ p);
view.setPadding(p,p,p,p);
}
private void screenMeasurement(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
sHeight = displayMetrics.heightPixels;
sWidth = displayMetrics.widthPixels;
}
public void onNavClick(View view){
moveBackground(view);
int d = Integer.parseInt(view.getTag().toString());
mViewPager.setCurrentItem(d);
}
private void moveBackground(View view){
int c = view.getLeft()+(view.getWidth()/2);
int left = c-(navBackground.getWidth()/2);
//navBackground.setX(left);
navBackground.animate().translationX(left);
}
public void GoToMenu(View view) {
edt_login = findViewById(R.id.edt_login);
edt_pw = findViewById(R.id.edt_pw);
Log.d("Test","PW: "+edt_pw.getText().toString());
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return RegisterFragment.newInstance();
case 2:
return LoginFragment.newInstance();
default:
return HomeFragment.newInstance();
}
}
@Override
public int getCount() {
return TOTAL_PAGE;
}
}
}
public类MainActivity扩展了AppCompatActivity{
私人最终整合式总页数=3;
私人住宅;
私人国际sWidth;
查看导航背景,导航底部;
查看导航主页、导航登录、导航注册;
编辑文本edt_pw,edt_登录;
ViewPager mViewPager;
私人部门SPAGERAAdapter MSECTIONSPAGERAAdapter;
公共静态字节[]getSHA(字符串输入)引发NoSuchAlgorithmException
{
MessageDigest md=MessageDigest.getInstance(“SHA-256”);
返回md.digest(input.getBytes(StandardCharsets.UTF_8));
}
公共静态字符串toHexString(字节[]散列)
{
BigInteger编号=新的BigInteger(1,哈希);
StringBuilder hexString=新的StringBuilder(编号:toString(16));
while(hexString.length()<32)
{
插入(0,'0');
}
返回hexString.toString();
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u启动);
SharedReferences pref=this.getSharedReferences(“系统设置”,0);//0-用于私有模式
SharedReferences.Editor=pref.edit();
如果((pref.getInt(“avi”,0))==0){
编辑:putInt(“avi”,1);
commit();
意向意向=新意向(此,指令.class);
星触觉(意向);
}
屏幕测量();
navBackground=findviewbyd(R.id.img\u nav\u bg);
navBottom=findViewById(R.id.navBottom);
navHome=findviewbyd(R.id.nav_home);
navLogin=findviewbyd(R.id.nav_login);
navRegister=findViewById(R.id.nav_寄存器);
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
mViewPager=(ViewPager)findViewById(R.id.ViewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
msectionspageradopter.getItem(2);
navBackground.post(新的Runnable(){
@凌驾
公开募捐{
int w=(int)(瑞士*2.5);
int h=w/2;
FrameLayout.LayoutParams bgParams=(FrameLayout.LayoutParams)navBackground.getLayoutParams();
bgParams.width=w;
bgParams.height=h;
//bgParams.leftMargin=-1*((w/2)-(sWidth/2));
bgParams.bottomMargin=(int)(-1*(h/2.5));
navBackground.requestLayout();
int left=(sWidth/2)-(w/2);
navBackground.setX(左);
resizeNavIcon(navHome,w);
resizeNavIcon(navLogin,w);
resizeNavIcon(navRegister,w);
}
});
}
私有void resizeNavIcon(视图,int-bgWidth){
int iconWidth=(bgWidth*43)/500;
int m=图标宽度/15;
LinearLayout.LayoutParams btnParams=(LinearLayout.LayoutParams)视图。getLayoutParams();
btnParams.width=iconWidth;
btnParams.height=iconWidth;
btnParams.leftMargin=(int)(m*1.5);
btnParams.rightMargin=(int)(m*1.5);
btnParams.topMargin=m;
btnParams.bottomMargin=m;
view.setLayoutParams(BTNParms);
view.requestLayout();
int p=(int)(iconWidth/3.5);
//Log.e(“标签”,“宽度:+iconWidth+”填充:+p);
设置填充(p,p,p,p);
}
私有度量(){
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
sHeight=displayMetrics.heightPixels;
sWidth=displayMetrics.widthPixels;
}
导航单击时的公共无效(视图){
移动背景(视图);
int d=Integer.parseInt(view.getTag().toString());
mViewPager.setCurrentItem(d);
}
私有void移动背景(视图){
int c=view.getLeft()+(view.getWidth()/2);
int left=c-(navBackground.getWidth()/2);
//navBackground.setX(左);
navBackground.animate().translationX(左);
}
公共void GoToMenu(视图){
edt\u登录=findViewById(R.id.edt\u登录);
edt_pw=findViewById(R.id.edt_pw);
Log.d(“Test”,“PW:+edt_PW.getText().toString());
}
公共类节SpagerAdapter扩展了FragmentPagerAdapter{
公共部分SpagerAdapter(碎片管理器fm){
超级(fm);
}
@凌驾
公共片段getItem(int位置){
开关(位置){
案例0:
返回RegisterFragment.newInstance();
案例2:
返回LoginFragment.newInstance();
违约:
返回HomeFragment.newInstance();
}
}
@凌驾
public int getCount(){
返回总页面;
}
}
}
activity_startup.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<ImageView
android:id="@+id/img_nav_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/nav_background"
android:layout_gravity="bottom"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<e.marco.gymdiary.MyViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="@+id/navbottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_gravity="bottom"
>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_login"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="0"
app:mi_shape="oval"/>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_home"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="1"
app:mi_shape="oval"/>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_register"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="2"
app:mi_shape="oval"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
行edt\u pw.getText().toString()
不返回任何内容。请解释清楚。多谢各位
溴
Marco根据您的代码
edt\u pw
在fragment\u login.xml中定义,您正在MainActivity.java中访问edt\u pw
,它肯定会返回null,您需要在LoginFragment中初始化它
我可以执行以下任一操作:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<ImageView
android:id="@+id/img_nav_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/nav_background"
android:layout_gravity="bottom"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<e.marco.gymdiary.MyViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="@+id/navbottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_gravity="bottom"
>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_login"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="0"
app:mi_shape="oval"/>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_home"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="1"
app:mi_shape="oval"/>
<com.ornach.magicicon.IconButton
android:id="@+id/nav_register"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="onNavClick"
android:padding="0dp"
app:mi_fontText=""
app:mi_iconColor="#FFF"
app:mi_typeface="font_awesome"
android:tag="2"
app:mi_shape="oval"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>