目前,由于
,我遇到了将数据传递到接口的麻烦 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)
问题是,我知道这主要是由于搞乱了布局ID,所以我检查了一切都很好。现在,我不知道我做错了什么。
以下是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()));
}
我的activity_main.xml文件在
下面<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"/>
我刚刚采取了另一种方法。我没有使用butterKnife,而是使用正常的方式绑定TextView,它仍然给我同样的错误。
以下是我的代码
的示例 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);
除此之外的所有内容都是相同的,对于那些想要检查整个代码的人来说,这是我在github中的项目https://github.com/ohmyskyhigh/StormyProfessional
答案 0 :(得分:1)
现在您已经发布了一些代码,很明显您使用的是ButterKnife,因此不需要手动绑定。
<击> 可能是以下问题之一:
findViewById
来获取实际实例。 =&GT;在为文本框使用实例变量之前,您需要在某处调用findViewById
。findViewById
以获取代码中对文本框的引用,但它会返回null
,因为找不到具有给定ID的视图=&gt;检查ID是否正常,在该行设置断点以检查相应的呼叫是否返回null
。也可能是您已更改ID但项目未重新编译。尝试一下干净的构建来检查。
击>我现在从您的代码中看到您正在使用线程进行UI更新。也许线程开始运行,而成员变量仍未绑定。您可以尝试更改此更新方法:
private void updateDisplay() {
if (mTemperatureValue != null)
mTemperatureValue.setText(mCurrent.getTemperature()+"");
if (mHumidityValue != null)
mHumidityValue.setText(mCurrent.getHumidity()+"%");
if (mPrecipChanceValue != null)
mPrecipChanceValue.setText(mCurrent.getPrecipProbability()+"%");
if (mSummaryText != null)
mSummaryText.setText(mCurrent.getSummary());
if (mTimeValue != null)
mTimeValue.setText(mCurrent.getFormattedTime());
mIconImage.setImageDrawable(getResources().getDrawable(mCurrent.getIconID()));
}
如果您在更改之后看到在某些时候您在文本框中获得了值,那么这就是问题所在。
如果您不看到任何值,则文本框未被ButterKnife正确绑定。
答案 1 :(得分:0)
发生这种情况的一件事是当您更改XML时..大多数时候您必须执行Project-&gt; Clean然后再尝试并且它可以正常工作。
答案 2 :(得分:-3)
在setText之前检查那些你要设置它的文本!null如果它的null然后产生空对象引用其他明智的用法就像这样
tv.setText(""+ here you want to set);