2015-12-21 152 views
-1

我正在开发一个android应用程序。我想通过自制类(entryClass)显示数据。列表由recyclerLayout表示,其中包括如此的cardView列表。 enter image description hereAndroid在某些情况下崩溃

对此的描述(Lorem Ipsum ...)将在最后一个单词的结尾处有200个字符(例如,如果第200个字符将在单词的中间,其余部分将被打印)做到这一点,我做了一个新的方法:

public static String cutText(String text){ 
    String temp=""; 
    StringBuilder build=new StringBuilder(temp); 
    int i=0; 
    while((i>200 || text.charAt(i)!=' ') && i<text.length()) 
    { 
     if(text.charAt(i)!='\n'){ 
     build.append(text.charAt(i));} 
     else {build.append(text.charAt(' '));} 
     i++; 
    } 
    return build.toString(); 
} 

但是,当我使用这种方法,应用程序在运行它崩溃。 这是添加了这种方法的一部分:

public void onBindViewHolder(final ViewHolder holder, int position) { 
    //creating an cardView 
     holder.mItem = mValues.get(position); 
     holder.mAuthorView.setText(mValues.get(position).author); 
     holder.mDateView.setText(mValues.get(position).date); 
     holder.mContentView.setText(cutText(mValues.get(position).content)); 
     holder.mImageView.setImageResource(R.drawable.sadness); 

当cutText法线更改为不包含它,一切工作正常。

我希望我包括一切重要的东西。 此致敬礼! 编辑: LOG FROM CRASH:

12-21 15:24:05.062 10574-10574/pl.spicyproject.quickstart E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: pl.spicyproject.quickstart, PID: 10574 
                     java.lang.StringIndexOutOfBoundsException: length=3; index=3 
                      at java.lang.String.charAt(Native Method) 
                      at pl.spicyproject.quickstart.Home.cutText(Home.java:201) 
                      at pl.spicyproject.quickstart.Home$SimpleItemRecyclerViewAdapter.onBindViewHolder(Home.java:151) 
                      at pl.spicyproject.quickstart.Home$SimpleItemRecyclerViewAdapter.onBindViewHolder(Home.java:129) 
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217) 
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250) 
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487) 
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363) 
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) 
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370) 
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) 
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) 
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900) 
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
                      at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732) 
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1497) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037) 
                      at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747) 
                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156) 
                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
                      at android.view.View.layout(View.java:16630) 
                      at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                      at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) 
                      at android.view.View.layout(View.jav 

12-21 15:24:07.450 10574-10574/pl.spicyproject.quickstart I /过程:发送信号。 PID:10574 SIG:9

+0

请包括发生崩溃时的logcat。 –

+0

http://pastebin.com/UY41yzuF – Unlucky

+1

你觉得'text.charAt('')'是什么意思? (我认为这意味着'text.charAt(32)')。 –

回答

2

您需要首先检查字符串长度while循环条件:

while(i<text.length() && (i>200 || text.charAt(i)!=' ')) 

,因为否则可能尝试调用text.charAt(i)i超过字符串的结尾。

另外:

build.append(text.charAt(' ')); 

这是否有至少33个字符是text只会工作,因为text.charAt(' ')是语义上等同于text.charAt(32)

目前还不清楚你想要做什么,但它看起来像你试图用空格替换换行符。下面可能会更容易:

text.replace('\n', ' ') 

为了您的规范,我认为这将是一个更加清晰,如果你使用类似下面的,这就避免了使用StringBuilder的:

String cutText(String text) { 
    int i = Math.min(200, text.length()); 
    while (i < text.length() && text.charAt(i) != ' ') { 
    ++i; 
    } 
    return text.substring(0, i).replace('\n', ' '); 
} 
+0

我做了这个改变。应用程序仍然崩溃。 – Unlucky

+0

完全一样吗? –

+0

public static String cutText(String text){ text = text.replace('\ n',''); String temp =“”; StringBuilder build = new StringBuilder(temp); int i = 0; (i 200 || text.charAt(i)!='')) { build.append(text.charAt(i)); i ++; } return build。的toString(); } – Unlucky

相关问题