我有一个包含3个片段的标签页。我在页面上有一个微调器。当用户从微调器中选择一个值时,3个片段将使用mysql数据进行更新。我有OnCreateView的问题,我正在膨胀布局,并返回视图,但在返回mainactivity后,“全局”视图变量被清除,当我尝试更新文本框时,我得到一个NPE,因为视图变量为null。
public class TabFragment1 extends Fragment {
public TabFragment1() {
// Required empty public constructor
}
View loadInflatedView;
TextView txtLoadNumber,txtAddlStops;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
loadInflatedView = inflater.inflate(R.layout.tab_fragment_1, container, false);
return loadInflatedView;
}
public void updateFragment1(Integer LoadNumber, Integer AddlStops){
txtLoadNumber = (TextView) loadInflatedView.findViewById(R.id.textViewLoadNumber);
txtAddlStops = (TextView) loadInflatedView.findViewById(R.id.textViewAddlStops);
//Log.d(TAG, "Update Dashboard Location");
txtLoadNumber.setText(LoadNumber);
txtAddlStops.setText(AddlStops);
}
和我的活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loads_tabbed);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Load"));
tabLayout.addTab(tabLayout.newTab().setText("Shipper"));
tabLayout.addTab(tabLayout.newTab().setText("Consignee"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(4);
final PagerAdapter adapter = new com.jrs.loadmanager.PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
...
//当我们从微调器中拍摄一个项目时,将执行此方法 @覆盖 public void onItemSelected(AdapterView parent,View view,int position,long id){
final Integer LoadNumber = parseInt( parent.getItemAtPosition(position).toString());
Cursor res = myDb.getLoadData(LoadNumber);
ShortcutBadger.removeCount(getApplicationContext());
session = new SessionManager(getApplicationContext());
if (res.moveToFirst()) {
Integer txtLoad = parseInt(res.getString(res.getColumnIndex(Config.TAG_LOADNUMBER)));
Integer txtStop = parseInt(res.getString(res.getColumnIndex(Config.TAG_ADDSTOPS)));
TabFragment1 FragVar = new TabFragment1();
FragVar.updateFragment1(txtLoad,txtStop);
在执行OnCreateView时设置了loadInflatedView,但在我尝试调用loadInflatedView.findViewById(R.id.textViewLoadNumber)时为null;
我已搜索并搜索过,无法找到解决该变量清除原因的方法。
这是致命的错误消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jrs.loadmanager, PID: 11045
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.jrs.loadmanager.TabFragment1.updateFragment1(TabFragment1.java:54)
at com.jrs.loadmanager.LoadsActivity.onItemSelected(LoadsActivity.java:426)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:3)
LocalBroadcastManager会帮你解决问题
向LocalBroadcastManager
fragment
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get data from intent and update
}
};
在mMessageReceiver
onCreate()
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive data.
// with actions named "my-event".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}
编写下面的代码,将活动数据更新为片段,这将在Activity
中编写Intent intent = new Intent("my-event");
// You can also include some extra data.
intent.putExtra("data", "my data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
不要忘记取消注册BroadcastReceiver
方法上的OnDestory()
答案 1 :(得分:2)
我认为问题是你的片段已被破坏。由于内存管理器已经破坏了你的片段。所以解决方案可能是
1。)您可以添加片段而不是替换它。 喜欢 -
fragmentTransaction.add(R.id.Main, homeFragment, "Home");
2.。)您可以在
中保存片段实例@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tag", TAG);
}
并从
恢复@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState)
someVarB = savedInstanceState.getString("tag");
}
请确保您已在xml布局中为视图提供了标记。
3.如果您正在使用View Pager以及标签布局,您可以试试这个。
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(4);
4.。)如果您使用的是ViewPager,请尝试使用
ViewPager pager = /* get my ViewPager */;
假设这实际上有东西
FragmentManager fm = getSupportFragmentManager();
pager.setAdapter(new FragmentStatePagerAdapter(fm) {
public int getCount() {
return titles.size();
}
public Fragment getItem(int position) {
MyFragment fragment = new MyFragment();
fragment.setTitle(titles.get(position));
return fragment;
}
public int getItemPosition(Object item) {
MyFragment fragment = (MyFragment)item;
String title = fragment.getTitle();
int position = titles.indexOf(title);
if (position >= 0) {
return position;
} else {
return POSITION_NONE;
}
}
});
并且您必须将其放在主要活动中或标签布局和视图寻呼机所在的位置。
答案 2 :(得分:0)
在我看来,由于你在线上进行的手动实例化,问题仍然存在
TabFragment1 FragVar = new TabFragment1();
稍作一点,片段对象在FragmentPagerAdapter
内创建并返回,然后由后者管理,据说可以很好地处理内存。
因此,当您在TabFragment1
内手动重新创建onItemSelected()
对象时,onCreateView()
的TabFragment1
未被解雇因此相应的片段视图不会膨胀。
与此同时,您正试图操纵尚未创建的TextView
对象(例如)!
这就是为什么我猜你正在获得NPE。
很简单的可能解决方案是使用Bundle
类来添加和接收额外内容。我想这应该有效!也许其他人可以添加它。 :)
希望这有帮助!