Android自定义适配器显示错误
我正在尝试创建自定义适配器listview 但是当我尝试启动应用程序时,应用程序崩溃并显示以下错误 我的main活动看起来像Android自定义适配器显示错误,android,android-listview,android-custom-view,Android,Android Listview,Android Custom View,我正在尝试创建自定义适配器listview 但是当我尝试启动应用程序时,应用程序崩溃并显示以下错误 我的main活动看起来像 package com.app.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.R.string; import android.os.Bundle; import android.app.Activity; import
package com.app.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ArrayList<Custom> fetch = new ArrayList<Custom>();
private ListView lv;
private CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Custom one = new Custom("Big1","Small1");
fetch.add(one);
lv = (ListView) findViewById(R.id.listview);
adapter = new CustomAdapter(MainActivity.this,
R.id.listview,
fetch);
lv.setAdapter(adapter);
}
}
CustomAdapter.java
package com.app.test;
public class Custom {
private String customBig;
private String customSmall;
public Custom(String string, String string2) {
this.customBig = string;
this.customSmall = string2;
}
public String getcustomBig() { return customBig; }
public void setcustomBig(String customBig) { this.customBig = customBig; }
public String getcustomSmall() { return customSmall; }
public void setcustomSmall(String customSmall) { this.customSmall = customSmall; }
}
package com.app.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Custom>{
private ArrayList<Custom> entries;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Custom> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.temp, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getcustomSmall());
}
return v;
}
}
你必须取消注释
//setContentView(R.layout.activity_main);
否则Android无法为
活动创建视图
层次结构
为什么您评论了设置内容视图(R.layout.Activity\u main)代码>活动中的行?嗨,还有一个疑问。如何通过循环向自定义添加值?当我尝试时,只显示最后一个值。你能帮忙吗?
package com.app.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Custom>{
private ArrayList<Custom> entries;
private Activity activity;
public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Custom> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.temp, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getcustomSmall());
}
return v;
}
}
01-08 16:23:10.878: E/AndroidRuntime(2704): FATAL EXCEPTION: main
01-08 16:23:10.878: E/AndroidRuntime(2704): Process: com.app.test, PID: 2704
01-08 16:23:10.878: E/AndroidRuntime(2704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.test/com.app.test.MainActivity}: java.lang.NullPointerException
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.os.Handler.dispatchMessage(Handler.java:102)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.os.Looper.loop(Looper.java:136)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-08 16:23:10.878: E/AndroidRuntime(2704): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 16:23:10.878: E/AndroidRuntime(2704): at java.lang.reflect.Method.invoke(Method.java:515)
01-08 16:23:10.878: E/AndroidRuntime(2704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-08 16:23:10.878: E/AndroidRuntime(2704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-08 16:23:10.878: E/AndroidRuntime(2704): at dalvik.system.NativeStart.main(Native Method)
01-08 16:23:10.878: E/AndroidRuntime(2704): Caused by: java.lang.NullPointerException
01-08 16:23:10.878: E/AndroidRuntime(2704): at com.app.test.MainActivity.onCreate(MainActivity.java:43)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.Activity.performCreate(Activity.java:5231)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-08 16:23:10.878: E/AndroidRuntime(2704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-08 16:23:10.878: E/AndroidRuntime(2704): ... 11 more
//setContentView(R.layout.activity_main);