2016-02-28 64 views
-4

我有一個應用程序與一個列表視圖打開一個活動,根據不同的字符串而改變。我可以將這些項目添加到個性化頁面。一切正常,但如果我使用「if」方法,它只能通過MainActivity打開活動。如果我打開喜歡的應用頁面崩潰我,給我的錯誤,如果「方法!我只是不明白,爲什麼我希望你能幫助我方法「如果」優先不工作

致命異常:!主

過程:COM .chiari.nicola.provaricetta,PID:6720 主題:主題:{} java.lang.NullPointerException:試圖調用虛擬方法 'boolean java.lang.String.equals(java.lang.Object)'null對象 參考 at 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 
+0

1.發佈所有堆棧跟蹤 – TheSunny

回答

0
  1. 後所有的堆棧跟蹤的
  2. 添加null檢查你的字符串,你嘗試和比較他們。

例如

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

我解決了感謝!!!我犯了一個愚蠢的錯誤!在添加這些代碼之前,我傾向於使用該項目......然後尋找無法提供的信息......無論如何,謝謝! – hulon