我正在構建一個android應用程序,該應用程序通過Internet加載json格式的文本並將其顯示在簡單列表中。 它工作得很好,但是當我替換一個字符串時,無論是哪一種,我的應用程序都會崩潰。你可以幫我嗎?當應用程序替換字符串時,android應用程序崩潰
下面是代碼:
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)
安置自己的logcat的。 –