나는 ListActivity
입니다. 시작될 때마다 AsyncTask
이 실행되어 내 IP 주소를 검색하고 그 결과를 ListActivity
에 표시합니다. 모든 것이 잘 작동합니다. 하지만 옵션 메뉴 (android 2.3)를 만들면 내 IP 주소가 ListActivity
에 표시되지 않습니다. AsyncTask
은 여전히 정상적으로 작동하며 메뉴 버튼을 클릭하면 옵션 메뉴가 올바르게 표시됩니다. 오류가 발생하지 않고 ListActivity
은 IP를 표시하지 않습니다.옵션 메뉴를 만들 때 ListActivity에 항목이 표시되지 않습니다.
public class MyIP extends ListActivity
{
ArrayList <Device> devicesList = new ArrayList<Device>();
AdapterListDevices adapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(android.R.id.list);
registerForContextMenu(listView);
adapter = new AdapterListDevices(MyIP.this, R.layout.row_list_devices, devicesList);
setListAdapter(adapter);
scanMyDevice();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
// Some code
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
// Some code
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.myip_options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Some code
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// Some code
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// Some code
}
public void updateListAdapter(Device myDevice)
{
devicesList.add(myDevice);
adapter.notifyDataSetChanged();
}
public void scanMyDevice()
{
ScanMyDeviceTask smd = new ScanMyDeviceTask(MyIP.this);
smd.execute();
}
}
내 ScanMyDeviceTask이 : : 예외가
try {
ScanMyDeviceTask smd = new ScanMyDeviceTask(MyIP.this);
smd.execute();
}
catch(Throwable t) {
Log.e("scanMyDevice", e.toString());
}
경우
public class ScanMyDeviceTask extends AsyncTask <Void, Void, Void>
{
Context context;
ProgressDialog progDialog;
Device myDevice;
InetAddress myAddress;
public ScanMyDeviceTask(Context context)
{
this.context = context;
progDialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute()
{
progDialog.setTitle("Searching...");
progDialog.setProgressStyle(progDialog.STYLE_SPINNER);
progDialog.show();
}
@Override
protected Void doInBackground(Void... params)
{
//Scan the ip address here
}
@Override
protected void onPostExecute(Void params)
{
MyIP act = (MyIP) context;
act.updateListAdapter(myDevice);
PublicData pd = (PublicData) act.getApplication();
pd.setMyIp(myAddress.getAddress());
progDialog.dismiss();
}
}
말씀하신대로 시도했지만 오류가 없습니다. AsyncTask가 시작되면 대화 상자가 표시되므로 AsyncTask가 올바르게 작동합니다. 작업이 끝나면 대화 상자가 닫힙니다. IP 주소는 Application 클래스에 저장됩니다. 나는 그들 모두를 검사 할 수있다. – user2859914
비동기 작업이 데이터가 변경되었음을 어댑터에 알 렸습니까? –
물론 가능합니다. 옵션 메뉴를 만들지 않으면 ListActivity에 내 IP 주소가 표시됩니다. – user2859914