我在FragmentPagerAdapter中有一堆片段,每个片段旁边有一个ImageView。如果我快速刷卡,则会出现此错误:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="jquery.js"></script>
<script type="text/javascript">
function dataRet (d){
console.log(d);
}
function ajaxreq(){
debugger
$.ajax({
url: *some link*
error: function(jqxhr) {
alert(jqxhr.responseText); // @text = response error, it is will be errors: 324, 500, 404 or anythings else
},
dataType:'html',
success: function(data) {
alert(data);
console.log(data);
var decoding = sDecodeURI(data);
console.log(decoding);
},
type: 'post'
})
function sDecodeURI(vsStr)
{
debugger
try {
if (typeof(vsStr) !== "string")
return vsStr;
var slSplit = vsStr.split('%u');
if (slSplit.length === 1)
return decodeURIComponent(vsStr);
var sResult = new StringBuilder(decodeURIComponent(slSplit[0]));
for (var i = 1; i < slSplit.length; ++i) {
var sTmp = slSplit[i];
var len = sTmp.length;
if (len < 4)
sResult.append("%u" + sTmp);
else {
var charCode = sTmp.substr(0, 4);
sResult.append(String.fromCharCode(parseInt(charCode,16)));
sResult.append(decodeURIComponent(sTmp.substr(4, sTmp.length-4)));
}
}
return sResult.toString();
}
catch (e) {
// Fallback to old-style encoding
return unescape(vsStr);
}
}
;
}
</script></head>
<body>
<input onclick="ajaxreq()" type="button" value="Click me to load info!"></input>
<div id="info"></div>
</body>
</html>
如果我慢慢滑动,那么它的效果非常好。
Android代码(每个片段的代码相同.AsyncTask中的代码):
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wilsapp.wilsapp, PID: 21319
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.wilsapp.wilsapp.Fragments.BuyerHomePageFragment9$DownloadImageTask.onPostExecute(BuyerHomePageFragment9.java:212)
at com.wilsapp.wilsapp.Fragments.BuyerHomePageFragment9$DownloadImageTask.onPostExecute(BuyerHomePageFragment9.java:192)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Android onCreatView方法:
protected void onPostExecute(Bitmap result) {
try {
ImageView img = (ImageView) getView().findViewById(R.id.ProductOneImageView);
img.setImageBitmap(result);
}catch (Exception e){
ImageView img = (ImageView) getView().findViewById(R.id.ProductOneImageView);
int id = getResources().getIdentifier("com.wilsapp.wilsapp:drawable/" + "error", null, null);
img.setImageResource(id);
}
}
我怎样才能避免NullPointerException?
答案 0 :(得分:0)
ImageView img = (ImageView) getView().findViewById(R.id.ProductFiveImageView);
ImageView需要在onCreateView中初始化。你的案例getView()
会返回null ...
答案 1 :(得分:0)
使用onViewCreated
方法而不是onCreateView
添加初始化代码段。它将确保在您的观看次数膨胀后初始化imageview
。
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
ImageView img = (ImageView) view.findViewById(R.id.ProductFiveImageView);
img.setImageBitmap(result);
}
答案 2 :(得分:0)
ImageView img = (ImageView) getView().findViewById(R.id.ProductFiveImageView);
hi您可以添加判断,getView是否为null,以及容错处理!