我有一个活动,已经制作了片段。在我的活动中,当我点击登录按钮时,加载器将显示。加载器变得不可见之后,我只需要附加该片段。该片段已在其他一些类中创建。我只需要在circular loader
变得不可见时附加该片段,并且我的片段已经在其他类中创建了,但我需要附加该片段。
MainActivity.java
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
if (loader_fragment != null) {
AVLoadingIndicatorView loader = loader_fragment.getView().findViewById(R.id.loder_login);
loader.setVisibility(View.INVISIBLE);
timer.cancel();
}
Home_Screen home_screen = (Home_Screen) getFragmentManager().findFragmentById(R.id.Home_screen_fragment);
fragmentManager1 = getFragmentManager();
fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.setCustomAnimations(
R.animator.slide_in_left,
R.animator.slide_out_left,
R.animator.slide_in_right,
R.animator.slide_out_right);
Home_screen_student home_screen_student = new Home_screen_student();
fragmentTransaction1.attach(R.id.Home_screen_fragment, home_screen_student);
fragmentTransaction1.commit();
}
};
HomeScreenFragment.java
package com.example.user.attendance;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Home_Screen extends Fragment implements OnNavigationItemSelectedListener {
DrawerLayout navigation_drawer;
NavigationView navigationView;
Button logout_Yes_button,logout_no_button, home_screen_take_attendance_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View Home_screen = inflater.inflate(R.layout.navigation_drawer,container,false);
android.support.v7.widget.Toolbar toolbar =
(android.support.v7.widget.Toolbar) Home_screen.findViewById(R.id.custom_action_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
navigation_drawer = (DrawerLayout) Home_screen.findViewById(R.id.drawer_layout);
navigationView = (NavigationView) Home_screen.findViewById(R.id.Navigation_view_for_teacher);
Logcat错误:
04-22 18:45:31.966 26404-26404 /? E / Zygote:没有v2 04-22 18:45:54.221 26404-26404 / com.example.user.attendance E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.user.attendance,PID:26404 java.lang.NullPointerException:尝试写入字段' android.app.FragmentManagerImpl android.app.Fragment.mFragmentManager'在null对象引用上 在android.app.BackStackRecord.doAddOp(BackStackRecord.java:461) 在android.app.BackStackRecord.replace(BackStackRecord.java:496) 在android.app.BackStackRecord.replace(BackStackRecord.java:488) at com.example.user.attendance.MainActivity $ 3 $ 2.run(MainActivity.java:181) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7402) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
帮助我。
答案 0 :(得分:1)
您在活动中使用viewPager吗? 您可能需要从一个活动处理到另一个活动 试试这个预告片:
ViewPager viewPager = (ViewPager) findViewById(R.id.rew1);
viewPager.setAdapter(new CustomPagerAdapter(this));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
//
public enum CustomPagerEnum {
HomeFragment (R.string.tab_text_1, R.layout.Home_screen_fragment),
private int mTitleResId;
private int mLayoutResId;
CustomPagerEnum(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
//
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
home.CustomPagerEnum customPagerEnum = home.CustomPagerEnum.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return home.CustomPagerEnum.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
home.CustomPagerEnum customPagerEnum = home.CustomPagerEnum.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
My language is German but I hope I understand your problem well!