、だけを更新するのはまったく問題ありませんでしたが、 fromTextView
を更新しようとすると、メソッド( )から取得し続けました。私は両方を試しまし
たが、問題は同じままです。私は何か見落としてますか?ListView
onReceive
NullPointerException
getView
BaseAdapter
handler.post
runOnUiThread
public class MainActivity extends Activity {
private SimpleBroadcastReceiver receiver;
private List<User> users;
private UserItemAdapter adapter;
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_service);
setup();
setupControls();
setupViews();
}
private void setupViews() {
users = new ArrayList<User>();
User chan = new User();
chan.setUsername("Username");
users.add(chan);
adapter = new UserItemAdapter(this, users);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
}
private void setupControls() {
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SimpleIntentService.class);
Toast.makeText(MainActivity.this, "Start Service", Toast.LENGTH_SHORT).show();
startService(intent);
}
});
}
private void setup() {
IntentFilter filter = new IntentFilter(SimpleBroadcastReceiver.ACTION_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new SimpleBroadcastReceiver(new Handler());
registerReceiver(receiver, filter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void updateUi(final List<User> data) {
this.runOnUiThread(new Runnable() {
public void run() {
for (User u : data) {
users.add(u);
}
adapter.notifyDataSetChanged();
}
});
}
public class SimpleBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_RESPONSE = "com.csun.intent.action.MESSAGE_PROCESSED";
private Handler handler;
public SimpleBroadcastReceiver(Handler handler) {
this.handler = handler;
}
@Override
public void onReceive(Context context, Intent intent) {
TextView t = (TextView) findViewById(com.csun.tutorplus.R.id.textview);
t.setText("Hello");
final ArrayList<User> data = (ArrayList<User>) intent.getSerializableExtra("users");
updateUi(data);
}
}
}
UserItemAdapter
public class UserItemAdapter extends BaseAdapter {
private static final String TAG = UserItemAdapter.class.getSimpleName();
private List<User> users;
private Context context;
private LayoutInflater inflater;
static class ViewHolder {
ImageView photo;
TextView username;
}
public UserItemAdapter(Context context, List<User> users) {
this.context = context;
this.users = users;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return users.size();
}
public Object getItem(int position) {
return users.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_user, null);
holder = new ViewHolder();
holder.photo = (ImageView) convertView.findViewById(R.id.item_user_XML_imageview_userphoto);
holder.username = (TextView) convertView.findViewById(R.id.item_user_XML_textview_username);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.username.setText(users.get(position).getUsername());
return convertView;
}
}
エラーログ
02-16 02:18:27.994: E/AndroidRuntime(26343): FATAL EXCEPTION: main
02-16 02:18:27.994: E/AndroidRuntime(26343): java.lang.NullPointerException
02-16 02:18:27.994: E/AndroidRuntime(26343): at com.csun.tutorplus.UserItemAdapter.getView(UserItemAdapter.java:54)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.ListView.makeAndAddView(ListView.java:1793)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.ListView.fillSpecific(ListView.java:1338)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.ListView.layoutChildren(ListView.java:1624)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.View.layout(View.java:7277)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.View.layout(View.java:7277)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.View.layout(View.java:7277)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.View.layout(View.java:7277)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.View.layout(View.java:7277)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.ViewRoot.performTraversals(ViewRoot.java:1203)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.os.Looper.loop(Looper.java:150)
02-16 02:18:27.994: E/AndroidRuntime(26343): at android.app.ActivityThread.main(ActivityThread.java:4369)
02-16 02:18:27.994: E/AndroidRuntime(26343): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 02:18:27.994: E/AndroidRuntime(26343): at java.lang.reflect.Method.invoke(Method.java:507)
02-16 02:18:27.994: E/AndroidRuntime(26343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:846)
02-16 02:18:27.994: E/AndroidRuntime(26343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
02-16 02:18:27.994: E/AndroidRuntime(26343): at dalvik.system.NativeStart.main(Native Method)