2行のデータを表示するカスタムリストビューアダプタがあります。このデータをに設定するString[] name = {some values};
ことはできますが、配列からリストビューを作成することはできません。アクティビティのコードは次のとおりです。
public class ListAction extends Activity implements OnClickListener {
String[] data = { "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data " };
String[] data2 = { "Sample User Data 2", "Sample User Data 2",
"Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
"Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
"Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
"Sample User Data 2", "Sample User Data 2", "Sample User Data 2",
"Sample User Data 2", "Sample User Data 2" };
ListView myList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
myList = (ListView) findViewById(R.id.listView);
myList.setAdapter((ListAdapter) new MyCustomAdapter());
myList.setCacheColorHint(0);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
String itemname = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(ListAction.this, ViewClient.class);
Bundle b = new Bundle();
b.putString("fileName", itemname);
intent.putExtras(b);
startActivity(intent);
}
});
}
class MyCustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return data.length;
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return data.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.list_item, parent, false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(data[position]);
TextView textLabel2 = (TextView) row.findViewById(R.id.text2);
textLabel2.setText(data2[position]);
return (row);
}
}
}
たとえば次のように、リストビューに配列からのデータを入力したいのですが
String[] data = getResources().getStringArray(R.array.categories);
、この行ではNullPointerしか取得できません。助けてください。
ログ:
04-06 23:35:00.110: E/AndroidRuntime(11420): FATAL EXCEPTION: main
04-06 23:35:00.110: E/AndroidRuntime(11420): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.ListAction}: java.lang.NullPointerException
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.os.Looper.loop(Looper.java:130)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-06 23:35:00.110: E/AndroidRuntime(11420): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420): at java.lang.reflect.Method.invoke(Method.java:507)
04-06 23:35:00.110: E/AndroidRuntime(11420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-06 23:35:00.110: E/AndroidRuntime(11420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-06 23:35:00.110: E/AndroidRuntime(11420): at dalvik.system.NativeStart.main(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420): Caused by: java.lang.NullPointerException
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
04-06 23:35:00.110: E/AndroidRuntime(11420): at com.example.ListAction.<init>(ListAction.java:25)
04-06 23:35:00.110: E/AndroidRuntime(11420): at java.lang.Class.newInstanceImpl(Native Method)
04-06 23:35:00.110: E/AndroidRuntime(11420): at java.lang.Class.newInstance(Class.java:1409)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-06 23:35:00.110: E/AndroidRuntime(11420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
04-06 23:35:00.110: E/AndroidRuntime(11420): ... 11 more
25行目はString[] data = getResources().getStringArray(R.array.categories);