Reputation: 1504
This code is successfully runnig as a left-to-right menu slider, I want to perform the onclick of ListView in menu, i tried various methods but was unsuccessful. I debug the code but control is not able to get into onClick.
public class MainActivity extends ListActivity {
ListView teams;
ArrayList<String> arr = new ArrayList<String>();
TextView tv0,tv1;
FlyOutContainer root; ///////class for slider
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv0 = (Button)findViewById(R.id.tv0);
tv1 = (Button)findViewById(R.id.tv1);
this.root =
(FlyOutContainer)this.getLayoutInflater().inflate(R.layout.activity_main, null);
this.setContentView(root);
teams=getListView();
arr.add("Introduction");
arr.add("Preface");
arr.add("Preamble");
arr.add("Contents");
arr.add("Articles");
arr.add("Schedules");
arr.add("Appendix");
arr.add("Amendment");
ArrayAdapter<String> adptr = new
ArrayAdapter<String>this,android.R.layout.simple_list_item_1,arr);
setListAdapter(adptr);
//
// arr.setOnItemClickListener(new OnItemClickListener() {
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
//
// Toast.makeText(getBaseContext(),getTitle(),Toast.LENGTH_SHORT).show();
// }
// });
/*
teams.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = teams.getId();
if(id==0)
{
tv0.setText("Introduction");
tv1.setText("India ie. Bharat is a Union of States.");
}
if(id==1)
{
tv0.setText("Preface");
}
if(id==2)
{
tv0.setText("Preamble");
}
}
});
*/
teams.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if(position==1)
{
tv0.setText("Introduction");
tv1.setText("India ie. Bharat is a Union of States.");
toggleMenu(arg1);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void toggleMenu(View v){
this.root.toggleMenu();
}
}
I tried much but get many errors !
can anyone give me a specific answer thanks...
LOGCAT:
12-03 13:00:37.031: E/AndroidRuntime(2690): FATAL EXCEPTION: main
12-03 13:00:37.031: E/AndroidRuntime(2690): java.lang.NullPointerException
12-03 13:00:37.031: E/AndroidRuntime(2690): at com.example.indianconstitution.MainActivity$1.onItemClick(MainActivity.java:88)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.widget.AbsListView$1.run(AbsListView.java:3463)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.os.Handler.handleCallback(Handler.java:730)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.os.Looper.loop(Looper.java:137)
12-03 13:00:37.031: E/AndroidRuntime(2690): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 13:00:37.031: E/AndroidRuntime(2690): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 13:00:37.031: E/AndroidRuntime(2690): at java.lang.reflect.Method.invoke(Method.java:525)
12-03 13:00:37.031: E/AndroidRuntime(2690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 13:00:37.031: E/AndroidRuntime(2690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 13:00:37.031: E/AndroidRuntime(2690): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 318
Reputation: 1504
Ok, I got the mistake:
tv0 & tv1 are declared as TextView but using
tv0 = (Button)findViewById(R.id.tv0);
instead
tv0 = (TextView)findViewById(R.id.tv0);
& rest of the code is absolutely fine
Upvotes: 0
Reputation: 1432
Something similar to this should work:
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(root);
tv0 = (Button)findViewById(R.id.tv0);
tv1 = (Button)findViewById(R.id.tv1);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_listview);
this.root =
(FlyOutContainer)this.getLayoutInflater().inflate(R.layout.activity_main, null);
teams=getListView();
arr.add("Introduction");
arr.add("Preface");
arr.add("Preamble");
arr.add("Contents");
arr.add("Articles");
arr.add("Schedules");
arr.add("Appendix");
arr.add("Amendment");
ArrayAdapter<String> dataAdapter = new
ArrayAdapter<String>(this,R.layout.simple_list_item_1,arr);
mDrawerListView.setAdapter(dataAdapter); //mDrawerListView is a ListView of your drawer
mDrawerListView.setOnItemClickListener(new DrawerItemClickListener());
And this declare the DrawerItemClickListener. Place this outside of onCreate()
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//do something according to position
switch (position) {
case 0: //do something
break;
case 1:
break;
}
}
}
Upvotes: 0
Reputation: 22306
You are trying to retrieve your tv0
and tv1
views before your content view is set. tv0 and tv1 will be null when you try to access them. Move your findViewById(...)
calls after you set the content view.. ex
super.onCreate(savedInstanceState);
this.root = (FlyOutContainer)this.getLayoutInflater().inflate(R.layout.activity_main, null);
this.setContentView(root);
tv0 = (Button)findViewById(R.id.tv0);
tv1 = (Button)findViewById(R.id.tv1);
Upvotes: 1