我正在构建一个Android应用程序,它通过互联网加载json格式的文本并将其显示在一个简单的列表中。 它工作得很好,但是当我替换一个字符串时,不管是什么类型,我的应用程序都会崩溃。你能救我吗?
以下是代码:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.ivMovieIcon = (ImageView)convertView.findViewById(R.id.ivIcon);
holder.tvMovie = (TextView)convertView.findViewById(R.id.tvMovie);
holder.tvTagline = (TextView)convertView.findViewById(R.id.tvTagline);
holder.tvYear = (TextView)convertView.findViewById(R.id.tvYear);
holder.tvDuration = (TextView)convertView.findViewById(R.id.tvDuration);
holder.tvDirector = (TextView)convertView.findViewById(R.id.tvDirector);
holder.rbMovieRating = (RatingBar)convertView.findViewById(R.id.rbMovie);
holder.tvCast = (TextView)convertView.findViewById(R.id.tvCast);
holder.tvStory = (TextView)convertView.findViewById(R.id.tvStory);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);
// Then later, when you want to display image
final ViewHolder finalHolder = holder;
ImageLoader.getInstance().displayImage(movieModelList.get(position).getImage(), holder.ivMovieIcon, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
finalHolder.ivMovieIcon.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
finalHolder.ivMovieIcon.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
finalHolder.ivMovieIcon.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
finalHolder.ivMovieIcon.setVisibility(View.INVISIBLE);
}
});
holder.tvMovie.setText(movieModelList.get(position).getMovie());
holder.tvTagline.setText(movieModelList.get(position).getTagline());
holder.tvYear.setText("Year: " + movieModelList.get(position).getYear());
holder.tvDuration.setText("Duration:" + movieModelList.get(position).getDuration());
holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector());
// rating bar
holder.rbMovieRating.setRating(movieModelList.get(position).getRating()/2);
StringBuffer stringBuffer = new StringBuffer();
for(MovieModel.Cast cast : movieModelList.get(position).getCastList()){
stringBuffer.append(cast.getName() + ", ");
}
holder.tvCast.setText("Cast:" + stringBuffer);
holder.tvStory.setText(movieModelList.get(position).getStory());
return convertView;
}
getDirector的get方法是:
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
当我改变这一行时:
holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector());
到
holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector().replace("test","test1"));
我的应用程序崩溃了。但为什么 ?我怎样才能替换此文本?
感谢您的帮助
这就是Logcat:
07-22 14:33:23.233 2714-2714/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.hisham.jsonparsingdemo.MainActivity$MovieAdapter.getView(MainActivity.java:219)
at android.widget.AbsListView.obtainView(AbsListView.java:2271)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:733)
at android.widget.ListView.layoutChildren(ListView.java:1622)
at android.widget.AbsListView.onLayout(AbsListView.java:2106)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
错误原因是NPE
(空指针异常)。
看起来你有来自数组列表的空数据。
如果getDirector()
返回null
holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector());
但是当你添加`replace(" test"," test1")部分时,如果movieModelList.get(position).getDirector()为null,它将崩溃。 / p>
您需要添加额外的Null检查或使用如下所示的TextUtils.isEmpty()方法:
if(!TextUtils.isEmpty(movieModelList.get(position).getDirector())){
holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector().replace("test","test1"));
}else{
holder.tvDirector.setText("Director:" + "write something else");
}
答案 1 :(得分:-1)
首先,如果我们从API获取值,则应始终进行空检查 在你的情况下它应该是这样的:
if(null != movieModelList && null != movieModelList.get(position) && null != movieModelList.get(position).getDirector()){
//here you can set text or do things this will help you avoid unnecessary crashes of data not available/null pointer from API
}
另外请使用大括号使代码易于阅读。 它将帮助其他人维护代码。
holder.tvDirector.setText("Director:" + (movieModelList.get(position).getDirector()).replace("test","test1"));
最后但并非最不重要 向我们提供您的日志,以便我们更好地帮助您 :)