2016-04-19 13 views
0

このエラーがなぜ発生するのか分かりません。私はこのコードにリンクしているすべてのコードをチェックしましたが、それでも解決策はありません。Nullオブジェクトリファレンス付きAndroid.os.Bundleでスタック

このようなIDEだけギミエラー:

のjava.lang.NullPointerException:nullオブジェクト上の仮想メソッドを呼び出すための試み 'int型android.os.Bundle.getInt(java.lang.Stringで)'私はそれがnullのオブジェクト参照を知っているが、それでも私はどこエラーの原因得ることはありませんcom.gook.rebill.fragment.HomeFragment.onAttachで (HomeFragment.java:74)

を参照。

私を助けてください。

は、これが私のコードです:私はHomeActivityで、フラグメントのコードを使用してここで

public class HomeFragment extends Fragment implements InternetConnectionListener, ApiHandler.ApiHandlerListener { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 
    private final int CATEGORY_ACTION = 1; 
    private CategorySelectionCallbacks mCallbacks; 
    private ArrayList<Category> categoryList; 
    private ListView categoryListView; 
    private String Error = null; 
    private InternetConnectionListener internetConnectionListener; 

    public HomeFragment() { 

    } 

    public static HomeFragment newInstance(int sectionNumber) { 
     HomeFragment fragment = new HomeFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((HomeActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER)); 
     try { 
      mCallbacks = (CategorySelectionCallbacks) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException("Activity must implement CategorySelectionCallbacks."); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_home, container, false); 
     categoryListView = (ListView) rootView.findViewById(R.id.categoryListView); 
     return rootView; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (UtilMethods.isConnectedToInternet(getActivity())) { 
      initCategoryList(); 
     } else { 
      internetConnectionListener = (InternetConnectionListener) HomeFragment.this; 
      showNoInternetDialog(getActivity(), internetConnectionListener, 
        getResources().getString(R.string.no_internet), 
        getResources().getString(R.string.no_internet_text), 
        getResources().getString(R.string.retry_string), 
        getResources().getString(R.string.exit_string), CATEGORY_ACTION); 
     } 

    } 

    public class getCategList extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 

      /** 
      * json is populating from text file. To make api call use ApiHandler class 
      * 
      * <CODE>ApiHandler apiHandler = new ApiHandler(this, URL_GET_CATEGORY);</CODE> <BR> 
      * <CODE>apiHandler.doApiRequest(ApiHandler.REQUEST_GET);</CODE> <BR> 
      * 
      * You will get the response in onSuccessResponse(String tag, String jsonString) method 
      * if successful api call has done. Do the parsing as the following. 
      */ 
      URL hp = null; 
      try { 
       hp = new URL(
         getString(R.string.liveurl) + "foodcategory.php"); 

       Log.d("URL", "" + hp); 
       URLConnection hpCon = hp.openConnection(); 
       hpCon.connect(); 
       InputStream input = hpCon.getInputStream(); 

       BufferedReader r = new BufferedReader(new InputStreamReader(input)); 

       String x = ""; 
       x = r.readLine(); 
       String total = ""; 

       while (x != null) { 
        total += x; 
        x = r.readLine(); 
       } 
       Log.d("UR1L", "" + total); 

       JSONArray j = new JSONArray(total); 

       Log.d("URL1", "" + j.length()); 



       categoryList = new ArrayList<Category>(); 

       for (int i = 0; i < j.length(); i++) { 
        Category category = new Category();// buat variabel category 
        JSONObject Obj; 
        Obj = j.getJSONObject(i); //sama sperti yang lama, cman ini lebih mempersingkat karena getJSONObject cm d tulis sekali aja disini 

        category.setId(Obj.getString(JF_ID)); 
        category.setTitle(Obj.getString(JF_TITLE)); 
        category.setIconUrl(Obj.getString(JF_ICON)); 

        if (!TextUtils.isEmpty(Obj.getString(JF_BACKGROUND_IMAGE))) { 
         category.setImageUrl(Obj.getString(JF_BACKGROUND_IMAGE)); 
        } 
        Log.d("URL1",""+Obj.getString(JF_TITLE)); 
        categoryList.add(category); 
       } 

       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         categoryListView.setAdapter(new CategoryAdapter(getActivity(), mCallbacks, categoryList)); 
        } 
       }); 



      }catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Error = e.getMessage(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Error = e.getMessage(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       Error = e.getMessage(); 
       e.printStackTrace(); 
      } catch (NullPointerException e) { 
       // TODO: handle exception 
       Error = e.getMessage(); 
      } 
      return null; 
     } 
    } 



    //! function for populate category list 
    private void initCategoryList() { 
      new getCategList().execute(); 
    } 

    @Override 
    public void onConnectionEstablished(int code) { 
     if (code == CATEGORY_ACTION) { 
      initCategoryList(); 
     } 
    } 

    @Override 
    public void onUserCanceled(int code) { 
     if (code == CATEGORY_ACTION) { 
      getActivity().finish(); 
     } 
    } 

    //! catch json response from here 
    @Override 
    public void onSuccessResponse(String tag, String jsonString) { 
     //! do same parsing as done in initCategoryList() 
    } 

    //! detect response error here 
    @Override 
    public void onFailureResponse(String tag) { 

    } 

    //! callback interface listen by HomeActivity to detect user click on category 
    public static interface CategorySelectionCallbacks { 
     void onCategorySelected(String catID, String title); 
    } 
} 

:あなたが添付したコードから

public void onSectionAttached(int number) { 
    switch (number) { 
     case 1: 
      mTitle = getString(R.string.title_section1); 
      break; 
     case 2: 
      mTitle = getString(R.string.title_section2); 
      break; 
     case 3: 
      mTitle = getString(R.string.title_section3); 
      break; 
    } 
} 
+0

どのようにあなたが '' HomeFragmentインスタンスを作成したのですか?コードを表示、 –

+0

あなたはおそらくそれを作成するためにnewInstanceメソッドを使用していないでしょう –

+0

私のHomeActivityコードを表示することを意味しますか?@ TungD.Nguyen? –

答えて

0

、ここで起こるように見えます。

 hp = new URL(
       getString(R.string.liveurl) + "foodcategory.php"); 

質問:Rとは何ですか?string.liveurlにアクセスしようとしているときにRをnullにすることはできますか?これはおそらく問題のようです。あなたはデバッガを使ってRが何であるかを調べることができますか?

+0

私はそこにはないと思うので、私はその行を削除すると、まだエラーを持っているので –

0

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     mCallbacks = (CategorySelectionCallbacks) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException("Activity must implement CategorySelectionCallbacks."); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_home, container, false); 
    categoryListView = (ListView) rootView.findViewById(R.id.categoryListView); 
    return rootView; 
} 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    ((HomeActivity) getActivity()).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER)); 
} 

編集1:

あなたがこの方法でHomeFragment置き換える必要があります。

HomeFragment homeFragment = HomeFragment.newInstance(my_int); 
_viewPagerAdapter.addFragments(homeFragment,"Home"); 

理由:は、単にあなたがArgumentsで代わりインスタンスのデフォルトコンストラクタでFragmentインスタンスを作成しました。

希望すると、これが役に立ちます。

+0

私はまだ..((HomeActivity)getActivity())onSectionAttached(getArguments()。getInt(ARG_SECTION_NUMBER) ); _ –

+0

@AdolfKohel、何のエラーが出ていますか? –

+0

まだヌルポインタ例外.. –

0

これを試してみてください -

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    try { 
     mCallbacks = (CategorySelectionCallbacks) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException("Activity must implement CategorySelectionCallbacks."); 
    } 
} 


    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 

      ((HomeActivity) getActivity()).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER)); 
} 
関連する問題