Android - ListActivity doesn't display item when create the options menu

816 views Asked by At

I have a ListActivity. Each time it starts, a AsyncTask runs to scan my IP address and displays the result in ListActivity. Everything works fine. But if I create a options menu (android 2.3) then my IP address doesn't be displayed in ListActivity. The AsyncTask still works ok and the options menu display correctly when I click on the menu button. No error occurs, the ListActivity just don't display the IP. Here's my code:

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();
    }
}

Here my ScanMyDeviceTask:

 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();

}

}

3

There are 3 answers

10
Daniel Gabriel On

Put a try-catch block around your code in scanMyDevice() like this:

try {
    ScanMyDeviceTask smd = new ScanMyDeviceTask(MyIP.this);
    smd.execute();
}
catch(Throwable t) {
    Log.e("scanMyDevice", e.toString());
}

If an exception is thrown in your AsyncTask your activity might not crash. Then you will see the behavior you described - everything seems to be working normally.

So adding the code above will help you see if there is an exception that happens there or not.

1
dangVarmit On

since the menu resource is a file, i think it needs to use all lowercase for the filename.

    inflater.inflate(R.menu.MyIP_options_menu, menu);

change the filename to use all lower case ( myip_options_menu.xml ) and then use R.menu.myip_options_menu in your inflation arg.

0
user2859914 On

After few days testing my source code again and again, I'm sure my source code is correct. And now I found the problem. To display items in ListActivity, I use a custom ArrayAdapter called AdapterListDevices (in separate file). You know, when I added options menu code in ListActivity, it didn't display anything. Then I used this trick:

  • Make some change in AdapterListDevices code. (example: type "blah" in anywhere)
  • Compile the project. (of course it failed, but not important)
  • Correct the AdapterListDevices code.
  • Compile the project.
  • Everything is ok, ListActivity displays items.

So, my problem is solved. But I have a stupid question: Why does my app go wrong if I change the source code in one file ? I don't use Eclipse, I use notepad and compile with ant in command promp. I also don't use emulator, I install the apk file in real phone. The new compiled app is installed over the old compiled app.