方法"如果"优先不工作

时间:2016-02-28 19:21:44

标签: java android

我有一个带有列表视图的应用程序,可以打开根据不同字符串更改的活动。我可以将项目添加到个性化页面。一切正常,但如果我使用" if"它只通过MainActivity打开活动就可以了。如果我打开最喜欢的应用程序页面崩溃我并给我错误,如果"方法!我只是不明白为什么!我希望你能帮助我!

致命的例外:主要

  

过程:com.chiari.nicola.provaricetta,PID:6720                                                                                 主题:主题:{}                                                                                 java.lang.NullPointerException:尝试调用虚方法   ' boolean java.lang.String.equals(java.lang.Object)'在null对象上   参考                                                                                     在com.chiari.nicola.provaricetta.Tab1.onCreateView(Tab1.java:68)

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v =inflater.inflate(R.layout.tab_1,container,false);

    Intent intent = getActivity().getIntent();
    String Titolo_ricetta = intent.getStringExtra("Titolo_ricetta");
    String Tempo_prep = intent.getStringExtra("Tempo_prep");
    String Immagine_url = intent.getStringExtra("Immagine_url");
    String Calore = intent.getStringExtra("Calore");
    String Glutine = intent.getStringExtra("Glutine");
    String Latticini = intent.getStringExtra("Latticini");
    String Uova = intent.getStringExtra("Uova");
    String Difficoltà = intent.getStringExtra("Difficoltà");
    String Kalorie = intent.getStringExtra("Kalorie");
    String Porzioni = intent.getStringExtra("Porzioni");
    String Costo = intent.getStringExtra("Costo");
    String Ingredienti = intent.getStringExtra("Ingredinti");
    String Consigli = intent.getStringExtra("Consigli");

    // make a TextView in activity_list_item_detail.xml called "name"
    TextView nameTextView = (TextView) v.findViewById(R.id.titolo);
    nameTextView .setText(Titolo_ricetta);
    ImageView Immage_url= (ImageView) v.findViewById(R.id.Immagine_url);
           Picasso.with(getActivity()).load(Immagine_url).fit().centerCrop().placeholder(R.drawable.piatto).error(R.drawable.x).into(Immage_url);

    ImageView Im_calore= (ImageView) v.findViewById(R.id.Im_calore);
    if (Calore.equals("c_no")) {
        Im_calore.setImageDrawable(getResources().getDrawable(R.drawable.fire));
    }else{}

    ImageView Im_glutine= (ImageView) v.findViewById(R.id.Im_glutine);
    if (Glutine.equals("g_no")) {
        Im_glutine.setImageDrawable(getResources().getDrawable(R.drawable.spiga_barrata));
    }else{}

    ImageView Im_latticini= (ImageView) v.findViewById(R.id.Im_latticini);
    if (Latticini.equals("l_no")) {
        Im_latticini.setImageDrawable(getResources().getDrawable(R.drawable.mucca));
    }else{}

    ImageView Im_uova= (ImageView) v.findViewById(R.id.Im_uova);
    if (Uova.equals("u_no")) {
        Im_uova.setImageDrawable(getResources().getDrawable(R.drawable.gallina));
    }else{}

    ImageView Im_difficoltà= (ImageView) v.findViewById(R.id.Im_difficoltà);
    if (Difficoltà.equals("bassa")) {Im_difficoltà.setImageDrawable(getResources().getDrawable(R.drawable.facile));}else if (Difficoltà.equals("media")) {Im_difficoltà.setImageDrawable(getResources().getDrawable(R.drawable.medio));}else if (Difficoltà.equals("alta")) {Im_difficoltà.setImageDrawable(getResources().getDrawable(R.drawable.difficile));}else{Im_difficoltà.setImageDrawable(getResources().getDrawable(R.drawable.x));}

    TextView tempo=(TextView) v.findViewById(R.id.tempo_ricetta);
    tempo.setText(Tempo_prep);

    TextView kalorie=(TextView) v.findViewById(R.id.kalorie);
    kalorie.setText(Kalorie);

    TextView porzioni=(TextView) v.findViewById(R.id.porzioni);
    porzioni.setText(Porzioni);

    ImageView Im_costo= (ImageView) v.findViewById(R.id.Im_costo);

    if (Costo.equals("basso")) {
        Im_costo.setImageDrawable(
                getResources().getDrawable(R.drawable.costo_basso));
    }else if (Costo.equals("medio")) {
        Im_costo.setImageDrawable(
                getResources().getDrawable(R.drawable.costo_medio));
    }else if (Costo.equals("alto")) {Im_costo.setImageDrawable(
            getResources().getDrawable(R.drawable.costoso));}
    else{Im_costo.setImageDrawable(
            getResources().getDrawable(R.drawable.x));}

    TextView ingredienti=(TextView) v.findViewById(R.id.ingredinti_txt);
    ingredienti.setText(Ingredienti);
    TextView consigli=(TextView) v.findViewById(R.id.consigli);
    consigli.setText(Consigli);

    String Scrittura_sezioni = getResources().getString(R.string.Scrittura_sezioni);

    Typeface intro=Typeface.createFromAsset(getContext().getAssets(), Scrittura_sezioni);
    TextView intro_text=(TextView)v.findViewById(R.id.intro);
    intro_text.setTypeface(intro);

    Typeface ingre=Typeface.createFromAsset(getContext().getAssets(), Scrittura_sezioni);
    TextView ingre_text=(TextView)v.findViewById(R.id.ingredienti);
    ingre_text.setTypeface(ingre);

preferiti.java

public class preferiti extends AppCompatActivity implements SearchView.OnQueryTextListener{

    public static final String ARG_ITEM_ID = "favorite_list";

    ListView favoriteList;
    SharedPreference sharedPreference;
    List<Ricette> countryList;

    Activity activity;
    CustomAdapter productListAdapter;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferiti);
        activity = this;



        //IMPOSTAZIONI TOOLBAR
        mToolbar = (Toolbar) findViewById(R.id.tool_bar_preferiti);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.mipmap.ic_keyboard_arrow_left_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        Typeface intro=Typeface.createFromAsset(getApplication().getAssets(), "Scrittura_titoli.ttf");
        TextView intro_text=(TextView) findViewById(R.id.titolo_dolci);
        intro_text.setTypeface(intro);
        //FINE IMPOSTAZIONI TOOLBAR



        // Get favorite items from SharedPreferences.
        sharedPreference = new SharedPreference();
        countryList = sharedPreference.getFavorites(activity);

        if (countryList == null) {
            showAlert(getResources().getString(R.string.no_favorites_items),
                    getResources().getString(R.string.no_favorites_msg));
        } else {

            if (countryList.size() == 0) {
                showAlert(
                        getResources().getString(R.string.no_favorites_items),
                        getResources().getString(R.string.no_favorites_msg));
            }

            favoriteList = (ListView) findViewById(R.id.lista_preferiti);
            if (countryList != null) {
                productListAdapter = new CustomAdapter(activity, (ArrayList<Ricette>) countryList);
                favoriteList.setAdapter(productListAdapter);










                favoriteList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View arg1,
                                            int position, long arg3) {

                        Ricette ricette = (Ricette) ((CustomAdapter) parent.getAdapter()).getItem(position);

                        Intent intent = new Intent(getApplication(), ListItemDetail.class);


                        intent.putExtra("Titolo_ricetta", ricette.getTitolo_ricetta());
                        intent.putExtra("Tempo_prep", ricette.getTempo_prep());
                        intent.putExtra("Immagine_url", ricette.getImmagine_url());
                        intent.putExtra("Calore",ricette.getCalore());
                        intent.putExtra("Glutine",ricette.getGlutine());
                        intent.putExtra("Latticini",ricette.getLatticini());
                        intent.putExtra("Uova",ricette.getUova());
                        intent.putExtra("Difficoltà",ricette.getDifficoltà());
                        intent.putExtra("Kalorie",ricette.getKalorie());
                        intent.putExtra("Porzioni",ricette.getPorzioni());
                        intent.putExtra("Costo",ricette.getCosto());
                        intent.putExtra("Ingredinti",ricette.getIngredinti());
                        intent.putExtra("Consigli",ricette.getConsigli());
                        intent.putExtra("Istruzione_1",ricette.getIstruzione_1());
                        intent.putExtra("Istruzione_2",ricette.getIstruzione_2());
                        intent.putExtra("Immagine_1",ricette.getImmagine_1());
                        intent.putExtra("Immagine_2",ricette.getImmagine_2());
                        intent.putExtra("Immagine_3",ricette.getImmagine_3());
                        intent.putExtra("Immagine_4",ricette.getImmagine_4());
                        intent.putExtra("Immagine_5",ricette.getImmagine_5());
                        intent.putExtra("Immagine_6",ricette.getImmagine_6());
                        intent.putExtra("Im_altr_1",ricette.getIm_altr_1());
                        intent.putExtra("Im_altr_2",ricette.getIm_altr_2());
                        intent.putExtra("Im_altr_3",ricette.getIm_altr_3());
                        intent.putExtra("Im_altr_4",ricette.getIm_altr_4());
                        intent.putExtra("Im_altr_5",ricette.getIm_altr_5());
                        intent.putExtra("Im_altr_6",ricette.getIm_altr_6());

                        startActivity(intent);

                    }
                });

                favoriteList
                        .setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

                            @Override
                            public boolean onItemLongClick(
                                    AdapterView<?> parent, View view,
                                    int position, long id) {

                                ImageView button = (ImageView) view
                                        .findViewById(R.id.imgbtn_favorite);

                                String tag = button.getTag().toString();
                                if (tag.equalsIgnoreCase("grey")) {
                                    sharedPreference.addFavorite(activity,
                                            countryList.get(position));
                                    Toast.makeText(
                                            activity,
                                            activity.getResources().getString(
                                                    R.string.add_favr),
                                            Toast.LENGTH_SHORT).show();

                                    button.setTag("red");
                                    button.setImageResource(R.drawable.heart_red);
                                } else {
                                    sharedPreference.removeFavorite(activity,
                                            countryList.get(position));
                                    button.setTag("grey");
                                    button.setImageResource(R.drawable.heart_grey);
                                    productListAdapter.remove(countryList
                                            .get(position));
                                    Toast.makeText(
                                            activity,
                                            activity.getResources().getString(
                                                    R.string.remove_favr),
                                            Toast.LENGTH_SHORT).show();
                                }
                                return true;
                            }
                        });
            }
        }

    }

    public void showAlert(String title, String message) {
        if (activity != null && !activity.isFinishing()) {
            AlertDialog alertDialog = new AlertDialog.Builder(activity)
                    .create();
            alertDialog.setTitle(title);
            alertDialog.setMessage(message);
            alertDialog.setCancelable(false);

            // setting OK Button
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            // activity.finish();
                            getFragmentManager().popBackStackImmediate();
                        }
                    });
            alertDialog.show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);


        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);

    }

    //IMPOSTAZIONI PER LA RICERCA
    @Override
    public boolean onQueryTextChange(String newText) {
        productListAdapter.getFilter().filter(newText);
        return false;}
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    //FINE IMPOSTAZIONI PER LA RICERCA

1 个答案:

答案 0 :(得分:0)

  1. 发布所有的stacktrace
  2. 在尝试比较它们之前,先对字符串添加空检查。
  3. e.g。

    if(Calore != null){
      if (Calore.equals("c_no"))