2017-07-22 65 views
-1

我正在構建一個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) 
+2

安置自己的logcat的。 –

回答

-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")); 

最後但並非最不重要 向我們提供您的日誌,以便我們可以幫助ü錯誤的更好 :)

0

原因是NPE(空指針異常)。

看起來你有數組列表中的空數據。

線之上,如果getDirector()返回null

holder.tvDirector.setText("Director:" + movieModelList.get(position).getDirector()); 

但是當你加上`替換( 「測試」, 「測試1」)的一部分,會不會崩潰,如果movieModelList.get(位置).getDirector()是空的,它會崩潰。

您需要添加額外的空支票或使用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"); 
}