【问题标题】:Android: Having trouble with custom arrayadapter. Force CloseAndroid:自定义阵列适配器有问题。强制关闭
【发布时间】:2014-07-08 16:26:14
【问题描述】:

当我尝试使用 listview 和自定义 arrayadapter 从 arraylist 获取数据运行活动时,我的应用程序不断强制关闭。

这里是自定义数组适配器代码:

import java.util.ArrayList;

import com.anshikka.solvexxx.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
public class PostAdapter extends ArrayAdapter<Post> {
private static class ViewHolder {
    TextView userName;
    TextView postContent;
    TextView timeStamp;
    ImageButton upVote;
    ImageButton downVote;
    TextView voteAmount;

}
public PostAdapter(Context context, ArrayList<Post> posts){
    super(context, 0, posts);
}

public View getView(int position, View convertView, ViewGroup parent){
    final Post post =  getItem(position);
    ViewHolder viewHolder;
    if(convertView == null){
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.solution_list_layout, parent, false);
        viewHolder.userName = (TextView) convertView.findViewById(R.id.userName);
        viewHolder.postContent = (TextView) convertView.findViewById(R.id.post);
        viewHolder.timeStamp = (TextView) convertView.findViewById(R.id.timestamp);
        viewHolder.upVote = (ImageButton) convertView.findViewById(R.id.upVoteButton);
        viewHolder.downVote = (ImageButton) convertView.findViewById(R.id.downVoteButton);
        viewHolder.voteAmount = (TextView) convertView.findViewById(R.id.voteAmount);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.userName.setText(post.getPostAuthor());
    viewHolder.postContent.setText(post.getPostContent());
    viewHolder.timeStamp.setText(post.getPostDate());
    viewHolder.upVote.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            post.voteUp();
        }

    });
    viewHolder.downVote.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            post.voteDown();
        }
    });
    viewHolder.voteAmount.setText(post.getPostVotes());

    return convertView;



}

}

这是我在 listView 中实现 post 适配器的活动类

public class TopAreaFeed extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.top_area_feed_activity);
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getActionBar().setCustomView(R.layout.abs_layout);
    ActionBar topAreaFeedBar = getActionBar();
    topAreaFeedBar.setBackgroundDrawable(new ColorDrawable(Color.RED));
    topAreaFeedBar.setDisplayShowTitleEnabled(false);
    topAreaFeedBar.setDisplayShowTitleEnabled(true);
    ArrayList<Post> posts = Post.getPosts();
    PostAdapter postAdapter = new PostAdapter(TopAreaFeed.this, posts);
    ListView postListView = (ListView) findViewById(R.id.listView1);
    postListView.setAdapter(postAdapter);


}

public boolean onCreateOptionsMenu(Menu menu){
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_viewer_actions, menu);
        return super.onCreateOptionsMenu(menu);

}
public void openComposeAction(){
    Intent composeIntent = new Intent(this, ComposePost.class);
    startActivity(composeIntent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_write_post:
            openComposeAction();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

这是我的自定义列表项

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_overlay" >

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:textStyle="bold"
        android:text="@string/userNameGoesHere"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/profilePic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/user" />


    <TextView
        android:id="@+id/timestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_alignParentLeft="true"
        android:text="@string/sampleTimeStamp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black_overlay"
        android:textSize="12sp" />

    <ImageButton
        android:id="@+id/upVoteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_toLeftOf="@+id/imageButton1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/arrow_up_float" />

    <ImageButton
        android:id="@+id/downVoteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/post"
        android:layout_marginRight="20dp"
        android:layout_marginTop="14dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/arrow_down_float" />

    <TextView
        android:id="@+id/voteAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:layout_marginLeft="21dp"
        android:text="@string/sampleUpVoteCount"
        android:textAppearance="?android:attr/textAppearanceSmall" />

 </RelativeLayout>

堆栈跟踪:

07-08 11:31:17.167: E/AndroidRuntime(8731): FATAL EXCEPTION: main
07-08 11:31:17.167: E/AndroidRuntime(8731): Process: com.anshikka.solvexxx, PID: 8731
07-08 11:31:17.167: E/AndroidRuntime(8731): android.content.res.Resources$NotFoundException: String resource ID #0x0
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.content.res.Resources.getText(Resources.java:268)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.TextView.setText(TextView.java:3923)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at com.anshikka.solvestream.PostAdapter.getView(PostAdapter.java:65)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.AbsListView.obtainView(AbsListView.java:2333)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.ListView.makeAndAddView(ListView.java:1854)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.ListView.fillDown(ListView.java:698)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.ListView.fillFromTop(ListView.java:759)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.ListView.layoutChildren(ListView.java:1673)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.AbsListView.onLayout(AbsListView.java:2145)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.View.layout(View.java:15273)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1069)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.View.layout(View.java:15273)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onLayout(FrameLayout.java:392)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.View.layout(View.java:15273)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.View.layout(View.java:15273)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.widget.FrameLayout.onLayout(FrameLayout.java:392)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.View.layout(View.java:15273)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewGroup.layout(ViewGroup.java:4763)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2057)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1814)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1044)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5749)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.Choreographer.doFrame(Choreographer.java:550)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.os.Handler.handleCallback(Handler.java:738)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.os.Looper.loop(Looper.java:135)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at android.app.ActivityThread.main(ActivityThread.java:5070)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at java.lang.reflect.Method.invoke(Native Method)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at java.lang.reflect.Method.invoke(Method.java:372)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-08 11:31:17.167: E/AndroidRuntime(8731):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

【问题讨论】:

  • 我们从哪里得到的?我对 android 编程相当陌生。
  • 日志猫。在此处发布异常部分
  • 您的 xml 中没有 ID 为 R.id.post 的 TextView 项目。您试图在 getView 函数中查找ViewById R.id.post

标签: android listview arraylist android-arrayadapter forceclose


【解决方案1】:

问题出在您的 adpater 类中。您为 textview 分配了无效的字符串值。您可以通过yourtextview.setText(String.valueOf(val))yourtextview.setText(tyourintvalue+""))进行设置

【讨论】:

    【解决方案2】:
    android.content.res.Resources$NotFoundException: String resource ID
    07-08 11:31:17.167: E/AndroidRuntime(8731): at android.content.res.Resources.getText(Resources.java:268) 
    07-08 11:31:17.167: E/AndroidRuntime(8731): at android.widget.TextView.setText(TextView.java:3923)
    

    看起来您在适配器getView() 中为 textview 设置了一个 int 值。

    有一个 setText 将 int 作为参数,但如果 android 找不到作为资源的 int,您最终会得到 ResourceNotFoundException。你需要的是一个以CharacterSequence 为参数的函数

    如果是这种情况,请使用textview.setText(String.valueOf(yourintvalue))

    这个

     viewHolder.voteAmount.setText(post.getPostVotes()); 
    

    应该是

      viewHolder.voteAmount.setText(String.valueOf(post.getPostVotes()));
    

    假设post.getPostVotes() 是整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多