I’m trying to call different activities depending on the item the user clicks on my listview but for some reason only the first element is working.
here is the code for the listview:
options = (ListView) findViewById(R.id.lstOptions);
ArrayAdapter<String> OPTADAP= new ArrayAdapter<String>(this,R.layout.optionslayout,OPTIONS);
options.setAdapter(OPTADAP);
options.setOnItemClickListener(new OnItemClickListener(){
Intent i;
@Override
public void onItemClick(AdapterView<?> parent, final View view,int pos, long id) {
switch(pos){
case 0:
i = new Intent(view.getContext(),Posting.class);
i.putExtra("usrid", usrdata.get("id"));
i.putExtra("usrname", usrdata.get("name"));
try{
startActivity(i);
} catch(ActivityNotFoundException e) {
e.printStackTrace();
}
break;
case 1:
i = new Intent(view.getContext(),WallActivity.class);
try{
startActivity(i);
}catch(ActivityNotFoundException e){
e.printStackTrace();
}
break;
default:
Toast.makeText(view.getContext(), "default", 10).show();
}
}
}
for some reason the block of code in case 1 does not run, i tried putting the code from case 0 in case 1 and it worked, so it has to be something wrong with this block:
i = new Intent(view.getContext(),WallActivity.class);
try{
startActivity(i);
}catch(ActivityNotFoundException e){
e.printStackTrace();
}
the WallActivity is basically just a webview:
public class WallActivity extends Activity{
WebView wv;
private final String wurl = "URL HERE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallactivity);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(wurl);
}
}
When i click on item 1 the code does not run, any ideas? all the help is greatly appreciated.
Why on earth are you catching the ActivityNotFoundException? If i had to hazard a guess, it woud be that you forgot to register WallActivity in your manifest and that exception is being thrown.
In any case, if you do catch an exception, in Android, if you want to see the stack trace you should write the exception to the Log instead of
e.printStackTrace()