2016-05-02 66 views
0

我能像this註釋處理,並按照此代碼:NullPointerExeption使用ButterKnife

public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse { 

@BindView(R.id.temperature_max) TextView temperature_max; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    ButterKnife.bind(this, view); 
    temperature_max.setText("NULL POINTER!"); 
... 
return view; 
} 
} 

我有複雜的結構:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView> 

<RelativeLayout> 

    <LinearLayout> 
    </LinearLayout> 

    <LinearLayout> 

     <RelativeLayout> 

      <RelativeLayout> 

       <TextView> 

       <TextView 
        android:id="@+id/temperature_max" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:textSize="20sp" /> 
      </RelativeLayout> 

      ... 
</ScrollView> 

這logcat的:

E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.my.app, PID: 22813 
               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.activity.MainActivity}: java.lang.NullPointerException 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283) 
                at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
                at android.os.Handler.dispatchMessage(Handler.java:102) 
                at android.os.Looper.loop(Looper.java:136) 
                at android.app.ActivityThread.main(ActivityThread.java:5158) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:515) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
                at dalvik.system.NativeStart.main(Native Method) 
               Caused by: java.lang.NullPointerException 
                at com.my.app.ui.fragment.MainFragment.onCreateView(MainFragment.java:28) 
                at android.app.Fragment.performCreateView(Fragment.java:1700) 
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 
                at android.app.BackStackRecord.run(BackStackRecord.java:685) 
                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 
                at android.app.Activity.performStart(Activity.java:5319) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)  
                at android.app.ActivityThread.access$800(ActivityThread.java:144)  
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)  
                at android.os.Handler.dispatchMessage(Handler.java:102)  
                at android.os.Looper.loop(Looper.java:136)  
                at android.app.ActivityThread.main(ActivityThread.java:5158)  
                at java.lang.reflect.Method.invokeNative(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:515)  
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)  
                at dalvik.system.NativeStart.main(Native Method)  

哦,是的,我使用這個gradle版本 - >'com.jakewharton:butterknife:8.0.1'

但我不知道爲什麼會出現NullPointerException。我通過互聯網搜索一個問題,但解決方案並沒有找到。有人幫我嗎?謝謝。

+0

對於'TextView temperature_max = ButterKnife.findById(view,R.id.temperature_max);' –

+0

有什麼問題如果他要使用它,基於他複雜的View Hierarchy會增加很多樣板。 – Sherlock

回答

0

我想你忘了返回你的查看

public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse { 

@BindView(R.id.temperature_max) TextView temperature_max; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    ButterKnife.bind(this, view); 
    temperature_max.setText("NULL POINTER!"); 
    return view; // This need to be return. 
} 
} 

編輯1:

@Bind取代@InjectView@InjectViewsButterKnife.bindButterKnife.unbind分別代替ButterKnife.injectButterKnife.reset

並且還加上這個。

TextView temperature_max = ButterKnife.findById(view, R.id.temperature_max); 

看這個參考Here.

+0

哦,不,我霧墨打印「返回視圖」在後。編輯帖子。 –

+0

@ D.Wayne檢查我編輯的答案。 –

-2

您應該使用@Bind代替@BindView。其他一切都很好。

+1

Butterknife 8.0.1使用'@ BindView'而不是'@ Bind' – Eixx

+0

@Eixx ok。我仍然在一些舊版本。謝謝你糾正我。 –