gpt4 book ai didi

android - 模拟器不给出错误,但设备在 putExtra() 上给出 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 17:54:50 26 4
gpt4 key购买 nike

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

我有一个带有选项卡和 3 个 fragment 的 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) ..

因此,在模拟器中一切正常,当我点击每个 fragment 时,它会显示正确的信息。

几天前将应用发布到 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)

我不明白,因为在模拟器中它可以正常工作而设备有错误,我不知道出了什么问题,请有人提出建议,需要维修,因为在商店里。

最佳答案

替换这个

public static final String MOD_SEARCH = null;

为此

public static String MOD_SEARCH = "";

我认为您的问题在于“final”一词,因为使用“null”初始化变量是一种“final”以后将无法更改其值,因此分配给 Intent 是 Null。

我希望你服务。

对不起,我的英语水平很低。

关于android - 模拟器不给出错误,但设备在 putExtra() 上给出 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com