ImageButton和NullPointerException

时间:2011-06-30 16:48:10

标签: android nullpointerexception imagebutton

我可以获得NullPointerException,因为我引用的按钮嵌套在几个布局中吗?每当我引用browseEditBtn时,我都会遇到异常。这是一个简单的按钮! 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>

源代码(在主onCreate()中调用Button)

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:ERROR / AndroidRuntime(9189):致命异常:主要   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):java.lang.RuntimeException:无法启动活动ComponentInfo {com.simplyDesign.mdk / com.simplyDesign.mdk.BrowseActivity}:java.lang.NullPointerException   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.os.Handler.dispatchMessage(Handler.java:99)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.os.Looper.loop(Looper.java:123)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在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):引起: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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   06-30 13:01:29.390:ERROR / AndroidRuntime(9189):... 11更多

edit_01

public void doAction(View v) {

    startActivity(new Intent(getApplicationContext(),
            EditItemActivity.class));

}

我过去曾使用其他按钮来指向EditItemActivity并且它们运行良好。

5 个答案:

答案 0 :(得分:1)

您发布的布局XML文件的名称是什么?它似乎不是browse.xml,因为它有一个ExpandableListView。您在Java代码中引用的ID必须出现在您设置的内容View中;否则,你必须自己给View充气。

修改

您可以添加类似

的内容
android:onClick="doAction"

到该ImageButton的XML。

然后,您的Activity中将有doAction(View v)方法执行onClick操作。

答案 1 :(得分:1)

您提供的布局似乎适用于R.layout.row。如果是这样,那么很有意义,你找不到按钮。为每行动态创建按钮。您可能想要创建自定义适配器,然后在getView中设置单击处理程序。

答案 2 :(得分:0)

试试这段代码,

 <ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    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>

您无法跳过layout_height和layout_width

答案 3 :(得分:0)

使用表格我遇到了接收内部元素的问题。尝试将表格作为一个整体findViewById(R.id.the_table)并使用myTableView.findViewById(R.id.a_row_in_the_table)并获取该行,然后使用myTableRow.findViewById(R.id.my_element_in_this_row)

答案 4 :(得分:0)

Button editBrowseBtn = (Button) findViewById(R.id.browseEditBtn);

您正在将ImageButton转换为Button。 ImageButton不是Button:

  

对象 - &gt;查看 - &gt; TextView - &gt;按钮

     

对象 - &gt;查看 - &gt; ImageView - &gt;   的ImageButton

所以试试这个:

ImageButton editBrowseBtn = (ImageButton) findViewById(R.id.browseEditBtn);