2017-01-21 47 views
-1

我有一個帶有複選框和重置按鈕的android片段。 裏面有一個更新和一個重置方法。每當我打電話這些methodes我得到一個NullPointerException。 我想不通爲什麼(這讓我一個新手)在調用方法時在android片段中獲取NullPointerException

package info.doktershuis.android.jichtcalculator; 


public class CalculatorFragment extends Fragment implements View.OnClickListener { 

double score = 0; 
double man = 2; 
double manPl = 0; 

public CalculatorFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

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

    CheckBox cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht); 
    cbGeslacht.setOnClickListener(this); 

    Button reset = (Button) layout.findViewById(R.id.button); 
    reset.setOnClickListener(this); 

    TextView punt = (TextView) layout.findViewById(R.id.tvPunt); 
    TextView perc = (TextView) layout.findViewById(R.id.tvPercentage); 
    TextView advies = (TextView) layout.findViewById(R.id.tvToelicht); 

    punt.setText(Double.toString(score)); 
    perc.setText(R.string.pt4); 
    perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
    advies.setText(R.string.pt4a); 

    return layout; 
} 

public void reset(View view) { 

    CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht); 

    TextView punt = (TextView) view.findViewById(R.id.tvPunt); 
    TextView perc = (TextView) view.findViewById(R.id.tvPercentage); 
    TextView advies = (TextView) view.findViewById(R.id.tvToelicht); 

    geslacht.setChecked(false); 

    double score = 0; 
    manPl = 0; 

    punt.setText(Double.toString(score)); 
    perc.setText(R.string.pt4); 
    perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
    advies.setText(R.string.pt4a); 

} 

public void update(View view) { 

    TextView punt = (TextView) view.findViewById(R.id.tvPunt); 
    TextView perc = (TextView) view.findViewById(R.id.tvPercentage); 
    TextView advies = (TextView) view.findViewById(R.id.tvToelicht); 

    score = manPl; 
    punt.setText(Double.toString(score)); 

    if (score <= 4) { 
     perc.setText(R.string.pt4); 
     perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
     advies.setText(R.string.pt4a); 
    } else if (score < 8) { 
     perc.setText(R.string.pt48); 
     perc.setBackgroundColor(getResources().getColor(holo_orange_light)); 
     advies.setText(R.string.pt48a); 
    } else { 
     perc.setText(R.string.pt8); 
     perc.setBackgroundColor(getResources().getColor(holo_red_light)); 
     advies.setText(R.string.pt8a); 
    } 

} 

@Override 
public void onClick(View view) { 

    CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht); 

    switch (view.getId()) { 
     case R.id.cbGeslacht: 
      if (geslacht.isChecked()) { 
       manPl = man; 
      } else { 
       manPl = 0; 
      } 
      update(view); 
      break; 
     case R.id.button: 
      reset(view); 
      break; 
    } 

} 

} 

當我提前

logcat的使用geslacht.setChecked(false);punt.setText(Double.toString(score)); 任何人可以幫助我這一個,非常感謝拋出異常:

01-21 08:43:25.233 23876-23876/info.doktershuis.android.jichtcalculator E/AndroidRuntime: FATAL EXCEPTION: main 
                         java.lang.NullPointerException 
                          at info.doktershuis.android.jichtcalculator.CalculatorFragment.reset(CalculatorFragment.java:111) 
                          at info.doktershuis.android.jichtcalculator.CalculatorFragment.onClick(CalculatorFragment.java:278) 
                          at android.view.View.performClick(View.java:4204) 
                          at android.view.View$PerformClick.run(View.java:17355) 
                          at android.os.Handler.handleCallback(Handler.java:725) 
                          at android.os.Handler.dispatchMessage(Handler.java:92) 
                          at android.os.Looper.loop(Looper.java:137) 
                          at android.app.ActivityThread.main(ActivityThread.java:5041) 
                          at java.lang.reflect.Method.invokeNative(Native Method) 
                          at java.lang.reflect.Method.invoke(Method.java:511) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                          at dalvik.system.NativeStart.main(Native Method) 

嗨ķNeeraj拉爾(SO-不準備幫助),我看了所有關於NullPointerException異常物品。找不到我的答案。請指出我在哪裏可以找到它。記得我是新手。我可能無法理解這一切。

+2

而不是 「R.string.pt4a」 使用的getString(R.string.pt4a)......在所有地方使用 「R.string.somthing」 –

+1

顯示logcat的輸出太。 –

回答

0

由於我沒有看到您的錯誤日誌,但仍然有幾個觀察可能導致空指針異常。

您每次都在使用按鈕視圖的按鈕點擊方法投射您的視圖。喜歡這裏

public void update(View view) { 

    TextView punt = (TextView) view.findViewById(R.id.tvPunt); 
    TextView perc = (TextView) view.findViewById(R.id.tvPercentage); 
    TextView advies = (TextView) view.findViewById(R.id.tvToelicht); 

這裏

public void reset(View view) { 

    CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht); 

    TextView punt = (TextView) view.findViewById(R.id.tvPunt); 
    TextView perc = (TextView) view.findViewById(R.id.tvPercentage); 
    TextView advies = (TextView) view.findViewById(R.id.tvToelicht); 

其更好地使這些意見全球,當你已經在你鑄造他們onCreateView方法從updatereset方法刪除這些行。讓我知道問題是否存在,或者發佈錯誤日誌。 :)

這裏是樣品

public class CalculatorFragment extends Fragment implements View.OnClickListener { 

    double score = 0; 
    double man = 2; 
    double manPl = 0; 

    CheckBox cbGeslacht; 

    TextView punt; 
    TextView perc; 
    TextView advies; 

    public CalculatorFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

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

     cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht); 
     cbGeslacht.setOnClickListener(this); 

     Button reset = (Button) layout.findViewById(R.id.button); 
     reset.setOnClickListener(this); 

     punt = (TextView) layout.findViewById(R.id.tvPunt); 
     perc = (TextView) layout.findViewById(R.id.tvPercentage); 
     advies = (TextView) layout.findViewById(R.id.tvToelicht); 

     punt.setText(Double.toString(score)); 
     perc.setText(R.string.pt4); 
     perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
     advies.setText(R.string.pt4a); 

     return layout; 
    } 

    public void reset(View view) { 

     cbGeslacht.setChecked(false); 

     double score = 0; 
     manPl = 0; 

     punt.setText(Double.toString(score)); 
     perc.setText(R.string.pt4); 
     perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
     advies.setText(R.string.pt4a); 

    } 

    public void update(View view) { 

     score = manPl; 
     punt.setText(Double.toString(score)); 

     if (score <= 4) { 
      perc.setText(R.string.pt4); 
      perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
      advies.setText(R.string.pt4a); 
     } else if (score < 8) { 
      perc.setText(R.string.pt48); 
      perc.setBackgroundColor(getResources().getColor(holo_orange_light)); 
      advies.setText(R.string.pt48a); 
     } else { 
      perc.setText(R.string.pt8); 
      perc.setBackgroundColor(getResources().getColor(holo_red_light)); 
      advies.setText(R.string.pt8a); 
     } 

    } 

    @Override 
    public void onClick(View view) { 


     switch (view.getId()) { 
      case R.id.cbGeslacht: 
       if (cbGeslacht.isChecked()) { 
        manPl = man; 
       } else { 
        manPl = 0; 
       } 
       update(view); 
       break; 
      case R.id.button: 
       reset(view); 
       break; 
     } 

    } 

} 
+0

我如何讓他們成爲全球? –

+0

請參閱已編輯的答案。 –

0

你可以用它代替取得按鈕的觀點爲獲得其他項目的佈局。

試試這個

public class CalculatorFragment extends Fragment implements View.OnClickListener { 

    double score = 0; 
    double man = 2; 
    double manPl = 0; 
    View layout; 
    public CalculatorFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     layout = inflater.inflate(R.layout.fragment_calculator, container, false); 

     CheckBox cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht); 
     cbGeslacht.setOnClickListener(this); 

     Button reset = (Button) layout.findViewById(R.id.button); 
     reset.setOnClickListener(this); 

     TextView punt = (TextView) layout.findViewById(R.id.tvPunt); 
     TextView perc = (TextView) layout.findViewById(R.id.tvPercentage); 
     TextView advies = (TextView) layout.findViewById(R.id.tvToelicht); 

     punt.setText(Double.toString(score)); 
     perc.setText(R.string.pt4); 
     perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
     advies.setText(R.string.pt4a); 

     return layout; 
    } 

    public void reset(View view) { 

     CheckBox geslacht = (CheckBox) layout.findViewById(R.id.cbGeslacht); 

     TextView punt = (TextView) layout.findViewById(R.id.tvPunt); 
     TextView perc = (TextView) layout.findViewById(R.id.tvPercentage); 
     TextView advies = (TextView) layout.findViewById(R.id.tvToelicht); 

     geslacht.setChecked(false); 

     double score = 0; 
     manPl = 0; 

     punt.setText(Double.toString(score)); 
     perc.setText(R.string.pt4); 
     perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
     advies.setText(R.string.pt4a); 

    } 

    public void update(View view) { 

     TextView punt = (TextView) layout.findViewById(R.id.tvPunt); 
     TextView perc = (TextView) layout.findViewById(R.id.tvPercentage); 
     TextView advies = (TextView) layout.findViewById(R.id.tvToelicht); 

     score = manPl; 
     punt.setText(Double.toString(score)); 

     if (score <= 4) { 
      perc.setText(R.string.pt4); 
      perc.setBackgroundColor(getResources().getColor(holo_green_light)); 
      advies.setText(R.string.pt4a); 
     } else if (score < 8) { 
      perc.setText(R.string.pt48); 
      perc.setBackgroundColor(getResources().getColor(holo_orange_light)); 
      advies.setText(R.string.pt48a); 
     } else { 
      perc.setText(R.string.pt8); 
      perc.setBackgroundColor(getResources().getColor(holo_red_light)); 
      advies.setText(R.string.pt8a); 
     } 

    } 

    @Override 
    public void onClick(View view) { 

     CheckBox geslacht = (CheckBox) layout.findViewById(R.id.cbGeslacht); 

     switch (view.getId()) { 
      case R.id.cbGeslacht: 
       if (geslacht.isChecked()) { 
        manPl = man; 
       } else { 
        manPl = 0; 
       } 
       update(view); 
       break; 
      case R.id.button: 
       reset(view); 
       break; 
     } 

    } 

} 
+0

謝謝,但是這給了Android Studio中的錯誤消息 –

相關問題