模拟器不提供错误但设备在putExtra()上给出了java.lang.NullPointerException

时间:2013-11-26 13:47:59

标签: android android-intent android-fragments nullpointerexception android-actionbar

我有一个大问题请有人帮帮我。

我有一个带Tabs和3片段的ActionBar。

onCreateView方法中我有这个:

MainActivity.java

...

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View rootView = null;
            TextView textView;
            final Intent intent = new Intent(getActivity(),ResultActivity.class);


            if(getArguments().getInt(ARG_SECTION_NUMBER)  == 1){
                rootView = inflater.inflate(R.layout.fragment_main, container, false);



                Button btn = (Button) rootView.findViewById(R.id.procura_ingredientes);
                final EditText ing_1 = (EditText) rootView.findViewById(R.id.ingredient_1);
                final EditText ing_2 = (EditText) rootView.findViewById(R.id.ingredient_2);


                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        intent.putExtra(MOD_SEARCH,1);

                        String ingredient_1 = ing_1.getText().toString();
                        String ingredient_2 = ing_2.getText().toString();

                        if(ingredient_1.length() <= 0) ingredient_1 = "";
                        if(ingredient_2.length() <= 0) ingredient_2 = "";

                        intent.putExtra("ingredient_1",ingredient_1);
                        intent.putExtra("ingredient_2",ingredient_2);

                        startActivity(intent);

                    }
                });


            }else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
                rootView = inflater.inflate(R.layout.fragment_two, container, false);
                GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
                final ImageAdapter imageAdapter = new ImageAdapter(getActivity());
                gridview.setAdapter(imageAdapter);

                gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                        //Toast.makeText(getActivity()," " + position, Toast.LENGTH_SHORT).show();

                        long cat = imageAdapter.getItemId(position);
                        if(cat <= 0) cat = 1;
                        intent.putExtra(MOD_SEARCH,2);
                        intent.putExtra("cat_id",cat);
                        startActivity(intent);

                    }
                });



            }else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
                rootView = inflater.inflate(R.layout.fragment_three, container, false);

                final EditText editText = (EditText)  rootView.findViewById(R.id.recipe_search_term);
                Button button = (Button) rootView.findViewById(R.id.submit_recipe_search);

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        int cont = editText.getText().toString().length();
                        if (cont < 2) {
                            AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                            dialog.setTitle("Atenção");
                            dialog.setMessage("O nome da receita precisa ter mais de " + cont + " letras ");

                            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.dismiss();
                                }
                            });
                            dialog.show();
                        } else {
                        intent.putExtra(MOD_SEARCH,0);
                        String recipe = editText.getText().toString();
                        if(recipe.length() <= 0) recipe = "";
                        intent.putExtra("recipe_name",recipe);
                        startActivity(intent);
                        }
                    }
                });
            }
            return rootView;
           /* textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));*/

        }
    }

如果你看,你会看到

if(getArguments().getInt(ARG_SECTION_NUMBER)  == 1){

}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){

else

这管理用户所在的选项卡,所有用户都转到相同的ResultActivity但是传递不同的ExtraStrings,

intent.putExtra(MOD_SEARCH,0);
intent.putExtra(MOD_SEARCH,1);
intent.putExtra(MOD_SEARCH,2);

MOD_SEARCH控制结果的执行情况。

ResultActivity.java

public static final String MOD_SEARCH = null;
mode  = getIntent().getIntExtra(MOD_SEARCH,0);

    if(mode == 0){ ..
     }else if(mode == 1) ..
    }else if(mode == 2) ..

所以,在模拟器中它工作正常,当我点击每个片段时它会显示正确的信息。

几天前将应用发布到Google Play后,我收到了以下错误:

    java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5558)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$2.onItemClick(MainActivity.java:256)

java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5325)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:226)

我不明白,因为在模拟器中它没有错误,设备有错误,我不知道有什么问题,请有人有建议,需要修理,因为在商店。

2 个答案:

答案 0 :(得分:2)

替换此

public static final String MOD_SEARCH = null;

这个

public static String MOD_SEARCH = "";

我认为你的问题是“final”这个词,因为初始化变量“null”是善的“final”以后无法改变它的值,所以分配给Intent是Null。

我希望你能服务。

抱歉我的英语水平很低。

答案 1 :(得分:1)

你没有在你的代码中的任何地方初始化Intent,因为它在这里给出了NullPointerException ..

intent.putExtra(MOD_SEARCH,2);
intent.putExtra("cat_id",cat);

将其视为本地化并在任何您想要的地方启动..