Could I get a NullPointerException because the button I am referencing is nested inside several layouts? Any time I reference browseEditBtn I get an exception. It’s a simple button! Come’on.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="*">
<!-- <include layout="@layout/mydesctextview" /> -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/txtItem"
android:textSize="20dip"
></TextView>
<ImageButton
android:src="@drawable/editbtn"
android:layout_marginRight="10dip"
android:layout_gravity="right|center_vertical"
android:id="@+id/browseEditBtn"
android:background="@drawable/my_group_statelist"
></ImageButton>
</TableRow>
</TableLayout>
</LinearLayout>
Source Code (The Button is called in the main onCreate()):
public class BrowseActivity extends ExpandableListActivity {
final private String[] asColumnsToReturn = {
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_ITEM,
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_DESC,
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_MANU,
Items.ITEMS_TABLE_NAME + "." + Items.ITEMS_ID };
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
DbHelper dbh = new DbHelper(this.getApplicationContext());
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
ExpandableListView browseView = (ExpandableListView) findViewById(android.R.id.list);
Button editBrowseBtn = (Button) findViewById(R.id.browseEditBtn);
Cursor mCursor = queryBuilder.query(db, asColumnsToReturn, null, null,
null, null, Items.DEFAULT_SORT_ORDER);
startManagingCursor(mCursor);
SimpleExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(
this, createGroup(), R.layout.row, R.layout.row, new String[] {
Items.ITEMS_ITEM, Items.ITEMS_DESC }, new int[] {
R.id.txtItem, R.id.dscItem }, createChildren(),
R.layout.exprow, new String[] { Items.ITEMS_DESC,
Items.ITEMS_MANU }, new int[] { R.id.dscItem,
R.id.manuItem });
browseView.setAdapter(mAdapter);
editBrowseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(BrowseActivity.this,
EditItemActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Item").setIntent(new Intent(this, AddItemActivity.class));
return super.onCreateOptionsMenu(menu);
}
@SuppressWarnings("rawtypes")
public List createGroup() {
DbHelper dbh = new DbHelper(this.getApplicationContext());
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
Cursor mCursor = queryBuilder.query(db, new String[] {
Items.ITEMS_ITEM, Items.ITEMS_DESC }, null, null, null, null,
Items.DEFAULT_SORT_ORDER);
startManagingCursor(mCursor);
ArrayList<HashMap<String, String>> groupList = new ArrayList<HashMap<String, String>>();
mCursor.moveToFirst();
while (!(mCursor.isAfterLast())) {
HashMap<String, String> groupMap = new HashMap<String, String>();
groupMap.put(Items.ITEMS_ITEM,
mCursor.getString(mCursor.getColumnIndex(Items.ITEMS_ITEM)));
groupMap.put(Items.ITEMS_DESC,
mCursor.getString(mCursor.getColumnIndex(Items.ITEMS_DESC)));
groupList.add(groupMap);
mCursor.moveToNext();
}
return (List) groupList;
}
@SuppressWarnings("rawtypes")
public List createChildren() {
DbHelper dbh = new DbHelper(this.getApplicationContext());
SQLiteDatabase db = dbh.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Items.ITEMS_TABLE_NAME);
Cursor mCursor = queryBuilder.query(db, new String[] {
Items.ITEMS_ITEM, Items.ITEMS_DESC, Items.ITEMS_MANU }, null,
null, null, null, Items.DEFAULT_SORT_ORDER);
startManagingCursor(mCursor);
mCursor.moveToFirst();
ArrayList<ArrayList<HashMap<String, String>>> childList = new ArrayList<ArrayList<HashMap<String, String>>>();
while (!(mCursor.isAfterLast())) {
ArrayList<HashMap<String, String>> childListDesc = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 1; i++) {
HashMap<String, String> childMap = new HashMap<String, String>();
childMap.put(Items.ITEMS_DESC, mCursor.getString(mCursor
.getColumnIndex(Items.ITEMS_DESC)));
childMap.put(Items.ITEMS_MANU, mCursor.getString(mCursor
.getColumnIndex(Items.ITEMS_MANU)));
childListDesc.add(childMap);
}
childList.add(childListDesc);
mCursor.moveToNext();
}
return (List) childList;
}
}
And finally the logcat, I think…
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): FATAL EXCEPTION: main
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplyDesign.mdk/com.simplyDesign.mdk.BrowseActivity}: java.lang.NullPointerException
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.os.Looper.loop(Looper.java:123)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at dalvik.system.NativeStart.main(Native Method)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): Caused by: java.lang.NullPointerException
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at com.simplyDesign.mdk.BrowseActivity.onCreate(BrowseActivity.java:62)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-30 13:01:29.390: ERROR/AndroidRuntime(9189): … 11 more
edit_01
public void doAction(View v) {
startActivity(new Intent(getApplicationContext(),
EditItemActivity.class));
}
I have used other buttons in the past to point to EditItemActivity and they have worked fine.
What is the name of the layout XML file that you posted? It doesn’t appear to be browse.xml because that one has your ExpandableListView in it. The IDs that you reference in your Java code must appear in the content
Viewthat you set; otherwise, you must inflate theViewyourself.Edit
You would add something like
to the XML of that ImageButton.
Then you would have a
doAction(View v)method in your Activity that would do your onClick actions.