当我尝试使用listview运行活动时,我的应用程序继续强制关闭,自定义数组适配器从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;
}
}
这是我将list适配器实现到listView
中的活动类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);
}
}
}
这是我的自定义listItem
<?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)
答案 0 :(得分:2)
问题出在您的适配器类中。您已为textview指定了无效的字符串值。您可以按yourtextview.setText(String.valueOf(val))
或yourtextview.setText(tyourintvalue+""))
答案 1 :(得分:0)
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()
为int。