ImageButton 和 NullPointerException
由于我引用的按钮嵌套在多个布局中,我是否可以获得 NullPointerException?每当我引用 browserEditBtn 时,我都会遇到异常。这是一个简单的按钮!快点。
<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>
源代码(按钮在主 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;
}
}
最后是 logcat,我认为......
06-30 13:01:29.390: 错误/AndroidRuntime(9189): 致命异常: main 06-30 13:01:29.390:错误/AndroidRuntime(9189):java.lang.RuntimeException:无法启动活动ComponentInfo {com.simplyDesign.mdk / com.simplyDesign.mdk.BrowseActivity}:java.lang.NullPointerException 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-30 13:01:29.390: 错误/AndroidRuntime(9189): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-30 13:01:29.390: 错误/AndroidRuntime(9189): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.os.Handler.dispatchMessage(Handler.java:99) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.os.Looper.loop(Looper.java:123) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.app.ActivityThread.main(ActivityThread.java:4627) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在java.lang.reflect.Method.invokeNative(本机方法) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在java.lang.reflect.Method.invoke(Method.java:521) 06-30 13:01:29.390: 错误/AndroidRuntime(9189): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在dalvik.system.NativeStart.main(本机方法) 06-30 13:01:29.390:错误/AndroidRuntime(9189):引起:java.lang.NullPointerException 06-30 13:01:29.390: 错误/AndroidRuntime(9189): 在 com.simplyDesign.mdk.BrowseActivity.onCreate(BrowseActivity.java:62) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-30 13:01:29.390:错误/AndroidRuntime(9189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 06-30 13:01:29.390: 错误/AndroidRuntime(9189): ... 11 更多
edit_01
public void doAction(View v) {
startActivity(new Intent(getApplicationContext(),
EditItemActivity.class));
}
我过去曾使用其他按钮指向 EditItemActivity,它们工作得很好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个代码,
你不能跳过layout_height和layout_width
Try this code,
You cannot skip layout_height and layout_width
使用表格时我在接收内部元素时遇到问题。尝试获取整个表格
findViewById(R.id.the_table)
并使用myTableView.findViewById(R.id.a_row_in_the_table)
并获取行,然后使用myTableRow.findViewById(R.id.my_element_in_this_row)
using tables i have had problems receiving inner elements. Try getting the table as a whole
findViewById(R.id.the_table)
and the usemyTableView.findViewById(R.id.a_row_in_the_table)
and get the row then usemyTableRow.findViewById(R.id.my_element_in_this_row)
您正在将 ImageButton 转换为 Button。 ImageButton 不是 Button:
所以试试这个:
You're casting the ImageButton to a Button. An ImageButton is not a Button:
So try this:
您发布的布局 XML 文件的名称是什么?它似乎不是 browser.xml,因为其中包含您的 ExpandableListView。您在 Java 代码中引用的 ID 必须出现在您设置的内容
View
中;否则,您必须自己膨胀View
。编辑
添加类似内容
您可以向该 ImageButton 的 XML
。然后,您的 Activity 中将有一个
doAction(View v)
方法来执行 onClick 操作。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
View
that you set; otherwise, you must inflate theView
yourself.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.您提供的布局似乎是针对 R.layout.row 的。如果是这样,那么您找不到该按钮就很有意义了。该按钮是为每一行动态创建的。您可能想要创建一个自定义适配器,然后在 getView 中设置单击处理程序。
It seems like the layout you provided is for R.layout.row. If so, then that makes a lot of sense that you can't find the button. The button is dynamically created for each row. You probably want to create a custom adapter and then in the getView, set the click handler.