尝试刷新片段给了我NullPointerException;我确保兼容性不是问题,并且我的片段不应为null [英] Attempt to refresh fragment gives me NullPointerException; I've made sure compatibility isn't an issue, and my fragments shouldn't be null

查看:57
本文介绍了尝试刷新片段给了我NullPointerException;我确保兼容性不是问题,并且我的片段不应为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在我的android项目中实现代码,在该项目中,按下按钮后片段会刷新,重新创建视图并显示不同的项目.我确保android.app和android.support.v4.app之间没有兼容性问题,并且肯定我的片段ID已正确标记.

I am currently trying to implement code in my android project where my fragments will refresh after a button is pressed, recreating the view and displaying different items. I made sure there were no compatibility issues between android.app and android.support.v4.app, and am positive that I have my fragments' id's labelled correctly.

这是在模拟器上运行应用程序时收到的确切错误:

Here is the exact error I received when running my app on an emulator:

  java.lang.NullPointerException: Attempt to write to field 'int      android.support.v4.app.Fragment.mNextAnim' on a null object reference
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:722)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是导致其类出现问题的Java代码:

And here is the java code that is causing the issue in its class:

package edu.sbu.cs.android.NMR.core;
import edu.sbu.cs.android.NMR.core.ImageAdapter;
import edu.sbu.cs.android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class HomeFragment extends Fragment {
     public interface OnSelectedListener {
           public void onSelected();
      }
    OnSelectedListener mListener;
     @Override
       public void onAttach(Activity activity) {
           super.onAttach(activity);
      try {
           mListener = (OnSelectedListener) activity;
       } catch (ClassCastException e) {
           throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
       }
   }

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

  View rootView = inflater.inflate(R.layout.fragment_home, container, false);
  gridview = (GridView)rootView.findViewById(R.id.gridView1);
    gridview.setAdapter(new ImageAdapter(gridview.getContext()));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            ((MainActivity) getActivity()).setProblem(position);
            switch (position) {
                case 0:
                    SpectraFragment.w.loadUrl("file:///android_asset/nmr0.html");
                    getArguments().putString("problem", "0");
                    FragmentManager fragMan;
                    fragMan = getFragmentManager();
                    Fragment frg= fragMan.findFragmentById(R.layout.fragment_spectra);
                    Fragment frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
                    final FragmentTransaction ft = fragMan.beginTransaction();
                    ft.detach(frg);
                    ft.attach(frg);
                    ft.detach(frg2);
                    ft.attach(frg2);
                    ft.commit();

                case 1:
                    SpectraFragment.w.loadUrl("file:///android_asset/nmr1.html");
                    getArguments().putString("problem", "1");
                    fragMan = getFragmentManager();
                    frg = fragMan.findFragmentById(R.layout.fragment_spectra);
                    frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
                    final FragmentTransaction ft2 = fragMan.beginTransaction();
                    ft2.detach(frg);
                    ft2.attach(frg);
                    ft2.detach(frg2);
                    ft2.attach(frg2);
                    ft2.commit();
                case 2:
                    SpectraFragment.w.loadUrl("file:///android_asset/nmr2.html");
                    getArguments().putString("problem", "2");
                    fragMan = getFragmentManager();
                    frg = fragMan.findFragmentById(R.layout.fragment_spectra);
                    frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
                    final FragmentTransaction ft3 = fragMan.beginTransaction();
                    ft3.detach(frg);
                    ft3.attach(frg);
                    ft3.detach(frg2);
                    ft3.attach(frg2);
                    ft3.commit();
                default:
                    SpectraFragment.w.loadUrl("file:///android_asset/nmr0.html");
            }

            Toast.makeText(getActivity(), " " + position, Toast.LENGTH_SHORT).show();
        }
    });
  return rootView;
   }
}

在不是由于android.support.v4.app和android.app之间的兼容性而导致的问题之前,有没有人遇到过这个问题,或者我是否在代码中遗漏了要引用空对象的地方?我在这些论坛中搜索了数周,并尝试了我能想到的所有解决方案.我甚至在MainActivity中将ActionBar选项卡切换为材质设计SlidingTabLayout来消除兼容性问题,但是它什么也没做.我对为什么我的代码无法使用感到沮丧,尽管我已经使用android一年了,但是尽管我开发了其他可以正常工作的应用程序,但仍然有些事情我不太了解.这一点比较复杂,已传递给我进行工作,因此我仍在弄清楚其中的一些工作原理.对不起,我感到很抱歉,但是我在这个问题上停留了一段时间,似乎已经走到了尽头.有人有解决方案吗?

Has anyone every had this issue before where the problem wasn't due to compatibility between android.support.v4.app and android.app, or am I missing a point in my code where I am making a null object reference? I've searched these forums for weeks and have tried every solution I can think of. I've even switched out ActionBar tabs in my MainActivity for a material design SlidingTabLayout to get rid of the compatibility issue, but it has done nothing. I'm frustrated as to why my code won't, and although I've been working with android for a year, there are still things that I don't quite understand although I've made a different app that works fine. This one is a bit more complex and was passed to me to work on, so I am still figuring out how some of it works. I'm sorry to have gone off on a rant, but I've been stuck on this issue for a while and seem to be at a dead end. Anyone have any solutions?

此外,如果在解决问题方面有任何不同,我正在使用android studio开发应用程序,但是我以前一直在使用eclipse并将项目移到这里.

Also, I'm using android studio to develop my app if that makes any difference in solving the issue, but I had been previously using eclipse and had moved the project over.

推荐答案

更新:在 switch 语句中,您缺少关于案例的 break; 语句.

Update: In the switch statement, you are missing the break; statements on your cases.

A glance at line 722 of the source code for BackStackRecord suggests you called FragmentTransaction.detach() with a null fragment. Probably one of your calls to findFragmentById() is returning null. To find out which one, check the result of each call and log a debug message if it is null.

这篇关于尝试刷新片段给了我NullPointerException;我确保兼容性不是问题,并且我的片段不应为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆