Currently, I am getting trouble of passing data to the interface, because of
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.miaor.stormyprofessional.UI.MainActivity$override.updateDisplay(MainActivity.java:112)
at com.example.miaor.stormyprofessional.UI.MainActivity$override.static$access$200(MainActivity.java:32)
at com.example.miaor.stormyprofessional.UI.MainActivity$override.access$dispatch(MainActivity.java)
at com.example.miaor.stormyprofessional.UI.MainActivity.access$200(MainActivity.java:0)
at com.example.miaor.stormyprofessional.UI.MainActivity$1$1$override.run(MainActivity.java:89)
at com.example.miaor.stormyprofessional.UI.MainActivity$1$1$override.access$dispatch(MainActivity.java)
at com.example.miaor.stormyprofessional.UI.MainActivity$1$1.run(MainActivity.java:0)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The thing is, I know this mainly caused by messing up with layout ID, so I checked it and everything is fine. Right now, I have no idea what I do wrong.
Here is the relevant code in MainActivity
private Current mCurrent;
@BindView(R.id.iconImage) ImageView mIconImage;
@BindView(R.id.SummaryText) TextView mSummaryText;
@BindView(R.id.temperatureValue) TextView mTemperatureValue;
@BindView(R.id.humidityValue) TextView mHumidityValue;
@BindView(R.id.PrecipChanceValue) TextView mPrecipChanceValue;
@BindView(R.id.timeValue) TextView mTimeValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
if (NetworkIsAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastURL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "failure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try{
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()){
mCurrent = getCurrentWeather(jsonData);
Log.i(TAG, mCurrent.getIconID()
+ "," + mCurrent.getHumidity()
+ "," + mCurrent.getTemperature()
+ "," + mCurrent.getPrecipProbability()
+ "," + mCurrent.getSummary()
+ "," + mCurrent.getFormattedTime());
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
}
else {
ErrorMessage();
}
}
catch (IOException | JSONException e){
Log.e(TAG, "Exception caught", e);
}
}
});
}
else {
Toast.makeText(this, R.string.deadNetwork,
Toast.LENGTH_LONG).show();
}
}
private void updateDisplay() {
mTemperatureValue.setText(mCurrent.getTemperature()+"");
mHumidityValue.setText(mCurrent.getHumidity()+"%");
mPrecipChanceValue.setText(mCurrent.getPrecipProbability()+"%");
mSummaryText.setText(mCurrent.getSummary());
mTimeValue.setText(mCurrent.getFormattedTime());
mIconImage.setImageDrawable(getResources().getDrawable(mCurrent.getIconID()));
}
And my activity_main.xml file down below
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UI.MainActivity"
android:background="@color/colorPrimaryDark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000"
android:id="@+id/temperatureValue"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="100sp"
android:textColor="@color/textColor"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/DegreeImage"
android:layout_alignTop="@+id/temperatureValue"
android:layout_toRightOf="@+id/temperatureValue"
android:layout_toEndOf="@+id/temperatureValue"
android:src="@drawable/degree"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00 AM"
android:id="@+id/timeValue"
android:layout_above="@+id/temperatureValue"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="@color/textColor"
android:layout_marginBottom="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/zhangjiagang_cn"
android:id="@+id/LocationText"
android:layout_above="@+id/timeValue"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="@color/textColor"
android:layout_marginBottom="80dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconImage"
android:layout_alignTop="@+id/temperatureValue"
android:layout_toLeftOf="@+id/temperatureValue"
android:layout_toStartOf="@+id/temperatureValue"
android:src="@drawable/sunny"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:layout_below="@+id/temperatureValue"
android:layout_centerHorizontal="true"
android:baselineAligned="false"
android:id="@+id/linearLayout">
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/humidity"
android:id="@+id/HumidityText"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/textColor"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="000%"
android:id="@+id/humidityValue"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/textColor"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rain_snow"
android:id="@+id/PrecipChanceText"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/textColor"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="000%"
android:id="@+id/PrecipChanceValue"
android:textSize="20sp"
android:textColor="@color/textColor"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blahblahblahblah"
android:layout_marginTop="40dp"
android:textSize="18sp"
android:textColor="@color/textColor"
android:id="@+id/SummaryText"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"/>
I just made another approach. Instead of using butterKnife, I used the normal way to bind the TextView and It still give me the same error.
Here is the example of my code
private Current mCurrent;
private TextView mTemperatureValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
TextView mTemperatureValue = (TextView)findViewById(R.id.temperatureValue);
Everything besides of this is the same, and for those who wants to check the whole code, here is my project in github https://github.com/ohmyskyhigh/StormyProfessional