我正在开发的应用程序包含带滑动标签的导航抽屉
在这里我需要将捆绑包从我的家庭活动中传递到导航抽屉,在收到捆绑包时我得到nullpointerexception
任何人都可以帮助我
这是我的家庭活动:
package android.lotus.com.androidmis;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.lotus.mis.modle.MyAppApplication;
import com.lotus.mis.modle.UserDetails;
public class Home extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Toolbar toolbar;
public static String UserName, userID, FlagList, User_code, Mail_id,
user_reporting_level;
UserDetails ObjUserDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
/**Getting bundle from the LotusLogin screen**/
Bundle bundle = getIntent().getExtras();
UserName = bundle.getString("key_username");
ObjUserDetails = (UserDetails) getIntent().getSerializableExtra("UserDetails");
/**getting user details from the UserDetails modle**/
userID = ObjUserDetails.getUserID();
User_code = ObjUserDetails.getEmpCode();
Mail_id = ObjUserDetails.getEmail();
user_reporting_level = ObjUserDetails.getPOSITION();
/**Setting user details as global user details*/
MyAppApplication mApp = new MyAppApplication();
mApp.setmGlobal_UserID(userID);
mApp.setMGlobal_EmpID(User_code);
mApp.setMGlobal_mailID(Mail_id);
//setting bundle to travels fragment
Bundle travels_bundle = new Bundle();
travels_bundle.putString("key_username", UserName);
travels_bundle.putString("key_userid", userID);
Travels travels_fragment = new Travels();
travels_fragment.setArguments(travels_bundle);
if (user_reporting_level.equalsIgnoreCase("-")) {
mApp.setmGlobal_reporting_level("");
} else {
mApp.setmGlobal_reporting_level(user_reporting_level);
}
FlagList = "3";
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new Travels()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
/* if (menuItem.getItemId() == R.id.nav_item_sent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new SentFragment()).commit();
}
*/
if (menuItem.getItemId() == R.id.nav_item_travels) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Travels()).commit();
/** Passing bundle to the travels fragment*/
Bundle travels_bundle = new Bundle();
travels_bundle.putString("key_username", UserName);
travels_bundle.putString("key_userid", userID);
Travels travels_fragment = new Travels();
travels_fragment.setArguments(travels_bundle);
} else if (menuItem.getItemId() == R.id.nav_item_sales) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Sales()).commit();
} else if (menuItem.getItemId() == R.id.nav_item_sales_reports) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new SalesReports()).commit();
} else if (menuItem.getItemId() == R.id.nav_item_voucher) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Voucher()).commit();
} else if (menuItem.getItemId() == R.id.nav_item_daylog) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Daylog()).commit();
} else if (menuItem.getItemId() == R.id.nav_item_leaves) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Leaves()).commit();
} else if (menuItem.getItemId() == R.id.nav_item_complaints) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new Complaints()).commit();
}
return false;
}
});
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我传递的家庭活动包
Bundle travels_bundle = new Bundle();
travels_bundle.putString("key_username", UserName);
travels_bundle.putString("key_userid", userID);
Travels travels_fragment = new Travels();
travels_fragment.setArguments(travels_bundle);
这是我的导航抽屉片段:
package android.lotus.com.androidmis;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.subtabs.travels.Cancelled;
import com.subtabs.travels.Today;
import com.subtabs.travels.Upcoming;
import com.subtabs.travels.History;
public class Travels extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;
public String UserName,UserID;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,container,false);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**Getting bundle from Home screen of Travels module*/
Bundle travels_bundle = this.getArguments(); //Error occuring place
UserName = travels_bundle.getString("key_username");
UserID = travels_bundle.getString("key_userid");
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 :
/**Sending bundle to the upcoming fragment of Travels module*/
Bundle upcoming_bundle = new Bundle();
upcoming_bundle.putString("key_username",UserName);
upcoming_bundle.putString("key_userid", UserID);
upcoming_bundle.putString("key_TravelFlag","1");
Upcoming upcoming_fragment = new Upcoming();
upcoming_fragment.setArguments(upcoming_bundle);
return new Upcoming();
case 1 : return new History();
case 2 : return new Today();
case 3 : return new Cancelled();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Upcoming";
case 1:
return "History";
case 2:
return "Today";
case 3:
return "Cancelled";
}
return null;
}
}
}
从这个导航抽屉片段中,我有4个滑动标签,并传递上面捆绑中收到的标签。
答案 0 :(得分:0)
请返回Fragment对象。
/**Sending bundle to the upcoming fragment of Travels module*/
Bundle upcoming_bundle = new Bundle();
upcoming_bundle.putString("key_username",UserName);
upcoming_bundle.putString("key_userid", UserID);
upcoming_bundle.putString("key_TravelFlag","1");
Upcoming upcoming_fragment = new Upcoming();
upcoming_fragment.setArguments(upcoming_bundle);
return upcoming_fragment; ////// Changed Here
并添加此代码 -
/**Getting bundle from Home screen of Travels module*/
Bundle travels_bundle = this.getArguments(); //Error occuring place
UserName = travels_bundle.getString("key_username");
UserID = travels_bundle.getString("key_userid");
设置适配器后。
修改强>
if (menuItem.getItemId() == R.id.nav_item_travels) {
/** Passing bundle to the travels fragment*/
Bundle travels_bundle = new Bundle();
travels_bundle.putString("key_username", UserName);
travels_bundle.putString("key_userid", userID);
Travels travels_fragment = new Travels();
travels_fragment.setArguments(travels_bundle);
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, travels_fragment).commit();
}