0

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

}

}

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
user2859914
  • 113
  • 1
  • 7

3 Answers3

0

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.

Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37
  • I tried as you said and there's no error. I'm sure AsyncTask works fine because when AsyncTask starts, a dialog will be displayed. When it finishes, the dialog will be closed. The IP address is saved in a Application class. I can check all of them. – user2859914 Dec 09 '13 at 21:21
  • Does your async task notify the adapter that the data has changed? – Daniel Gabriel Dec 09 '13 at 21:25
  • Yes, of course. The ListActivity displays my IP address when I don't create the options menu. – user2859914 Dec 09 '13 at 21:28
  • Here's my ScanMyDeviceTask: – user2859914 Dec 09 '13 at 21:47
  • Please edit your question and post the code there. Also, what you posted only has `onPreExecute()`, and I don't see where you call `adapter.notifyDataSetChanged()`. – Daniel Gabriel Dec 09 '13 at 21:49
  • sorry, I also upload the updateListAdapter() code in MyIP, above scanMyDevice(). Please see the main activity. – user2859914 Dec 09 '13 at 21:56
  • Got it. Scratch my answer. You should debug by putting a break point into `onPostExecute()` and `updateListAdapter()` to make sure they are properly called. Also check that the device that the task scanned is passed in properly. This way you will at least narrow down the places to look for a problem. – Daniel Gabriel Dec 09 '13 at 21:58
  • I did what you said. The updateListAdapter() was really called. The scan task in doInBackground() works fine, because my IP address was saved in PublicData. – user2859914 Dec 09 '13 at 22:17
  • Did you see in the debugger that these 2 lines were executed? And did you check that `myDevice` is valid? `devicesList.add(myDevice); adapter.notifyDataSetChanged();` – Daniel Gabriel Dec 09 '13 at 22:27
  • Give me one day to re-check all things again. Thanks for your help. – user2859914 Dec 09 '13 at 22:44
0

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.

dangVarmit
  • 5,641
  • 2
  • 22
  • 24
  • Oh, I'm sorry. The filename is actually in lowercase. I changed the filename when upload source code here for many reason. – user2859914 Dec 09 '13 at 21:31
0

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.

user2859914
  • 113
  • 1
  • 7