应用程序在findViewById()崩溃

时间:2014-06-21 03:34:15

标签: android listview android-listview textview getview

所以我在activity_main.xml中有一个ListView。也是不同xml da_item.xml中的T​​extView。 textview用于各个listview项目。

现在我的代码中有一个 onContextItemSelected()函数,它调用 bucketMarkItem(long itemTitle) bucketMarkItem(long itemTitle)使用函数 colorCorrection()更改最后一项的背景颜色。

整个代码工作正常。

但是我有一个带有 enterItem() onClick监听器的按钮。我从 enterItem()中调用相同的 colorCorrection(),然后应用崩溃了。 它崩溃了。

TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv);

我不明白为什么。

我见过很多像这样的问题,答案是使用 setContentView(R.layout.activity_main),但我已经把它包括在内了。

onContextItemSelected Code

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    super.onContextItemSelected(item);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    long hula = info.id;

    if(item.getTitle()=="Mark as Done"){
        bucketMarkItem(hula);
    }
    if(item.getTitle()=="Mark as Undone"){
        bucketUnMarkItem(hula);
    }
    if(item.getTitle()=="Delete Item"){
        bucketDeletelistItem(hula);
    }

    return true;
}

bucketMarkItem()代码

private void bucketMarkItem(long itemTitle) {
    // TODO Auto-generated method stub
    View listItemBla=(View)list.getChildAt((int)(long)itemTitle);
    TextView txtDep=(TextView)listItemBla.findViewById(R.id.tv);
    txtDep.setBackgroundColor(Color.GREEN);

    String txtDepText = ""+txtDep.getText();
    String[] result = txtDepText.split(" ");
    String result1 = result[1];

    Cursor cursor = myDB.getAllRows();

    if(cursor.moveToFirst()){
        do{
        if(result1.equals(cursor.getString(1))){
            long yourDBID = yourDB.insertRow(cursor.getInt(0));
        }
        }while(cursor.moveToNext());
    }
    cursor.close();
    colorCorrecton();
}

colorCorrecton代码

private void colorCorrecton() {

    Toast.makeText(MainActivity.this,""+ list.getCount(), Toast.LENGTH_SHORT).show();

    View listItemBla1=(View)list.getChildAt((list.getCount()-1));
    TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv);

    Toast.makeText(MainActivity.this, ""+txtDep1.getText(), Toast.LENGTH_SHORT).show();
    txtDep.setBackgroundColor(Color.GREEN);
}

enterItem()代码

public void enterItem(View v) {
// TODO Auto-generated method stub
EditText dataEntry_EditText=(EditText)findViewById(R.id.editText1);
Editable dataEntry_Editable = dataEntry_EditText.getText();
String dataEntry_Finaldata = dataEntry_Editable.toString();
long newId = myDB.insertRow(dataEntry_Finaldata, 233, "red");

//Toast.makeText(MainActivity.this, dataEntry_Finaldata, Toast.LENGTH_LONG).show();
dataEntry_EditText.setText("");

populateListView();

//Toast.makeText(MainActivity.this, ""+list.getCount(), Toast.LENGTH_LONG).show();
colorCorrecton();
}

完整代码

package com.example.bucketlist;

    import java.util.ArrayList;

    import android.support.v4.app.FragmentActivity;
    import android.text.Editable;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends FragmentActivity {

ListView list;
DBAdapter myDB;
DBAdapter1 yourDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView bucketMenu = (ImageView)findViewById(R.id.bucketMenuW);
    list= (ListView)findViewById(R.id.listView1);
    registerForContextMenu(list);
    openDB();
    populateListView();
    //colorCorrecton();

}

private void colorCorrecton() {

    Toast.makeText(MainActivity.this,""+ list.getCount(), Toast.LENGTH_SHORT).show();

    View listItemBla1=(View)list.getChildAt((list.getCount()-1));
    TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv);

    Toast.makeText(MainActivity.this, ""+txtDep1.getText(), Toast.LENGTH_SHORT).show();
    //txtDep.setBackgroundColor(Color.GREEN);
}

public void conMenu(View v) {
    // TODO Auto-generated method stub
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);

    if(v.getId()==R.id.listView1){
        menu.add("Mark as Done");
        menu.add("Mark as Undone");
        menu.add("Delete Item");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    super.onContextItemSelected(item);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    long hula = info.id;

    if(item.getTitle()=="Mark as Done"){
        bucketMarkItem(hula);
    }
    if(item.getTitle()=="Mark as Undone"){
        bucketUnMarkItem(hula);
    }
    if(item.getTitle()=="Delete Item"){
        bucketDeletelistItem(hula);
    }

    return true;
}

private void bucketUnMarkItem(long itemTitle) {
    // TODO Auto-generated method stub
    View listItemBla=(View)list.getChildAt((int)(long)itemTitle);
    TextView txtDep=(TextView)listItemBla.findViewById(R.id.tv);
    String txtDepText = ""+txtDep.getText();
    String[] result = txtDepText.split(" ");
    String result1 = result[1];

    Cursor cursor = myDB.getAllRows();

    int markID=0;
    if(cursor.moveToFirst()){
        do{
        if(result1.equals(cursor.getString(1))){
            markID = cursor.getInt(0);
        }
        }while(cursor.moveToNext());
    }
    cursor.close();
    //Toast.makeText(MainActivity.this, ""+list.getCount(), Toast.LENGTH_LONG).show();

    Cursor cursor1 = yourDB.getAllRows();

    if(cursor1.moveToFirst()){
        do{
        if(markID == cursor1.getInt(1)){
            boolean newid2 = yourDB.deleteRow(cursor1.getInt(0));
        }
        }while(cursor1.moveToNext());
    }
    cursor1.close();

    colorCorrecton();
}

private void bucketMarkItem(long itemTitle) {
    // TODO Auto-generated method stub
    View listItemBla=(View)list.getChildAt((int)(long)itemTitle);
    TextView txtDep=(TextView)listItemBla.findViewById(R.id.tv);
    txtDep.setBackgroundColor(Color.GREEN);

    String txtDepText = ""+txtDep.getText();
    String[] result = txtDepText.split(" ");
    String result1 = result[1];

    Cursor cursor = myDB.getAllRows();

    if(cursor.moveToFirst()){
        do{
        if(result1.equals(cursor.getString(1))){
            long yourDBID = yourDB.insertRow(cursor.getInt(0));
        }
        }while(cursor.moveToNext());
    }
    cursor.close();
}

public void deleteItem(View v) {
    // TODO Auto-generated method stub
    myDB.deleteAll();
    yourDB.deleteAll();
    populateListView();
}

private void bucketDeletelistItem(long itemTitle) {
    View listItemBla=(View)list.getChildAt((int)(long)itemTitle);
    TextView txtDep=(TextView)listItemBla.findViewById(R.id.tv);
    String txtDepText = ""+txtDep.getText();
    String[] result = txtDepText.split(" ");
    String result1 = result[1];

    Cursor cursor = myDB.getAllRows();

    int markID=0;
    if(cursor.moveToFirst()){
        do{
        if(result1.equals(cursor.getString(1))){
            boolean newid1 = myDB.deleteRow(cursor.getInt(0));
            markID = cursor.getInt(0);
        }
        }while(cursor.moveToNext());
    }
    cursor.close();
    populateListView();
    //Toast.makeText(MainActivity.this, ""+list.getCount(), Toast.LENGTH_LONG).show();

    Cursor cursor1 = yourDB.getAllRows();

    if(cursor1.moveToFirst()){
        do{
        if(markID == cursor1.getInt(1)){
            boolean newid2 = yourDB.deleteRow(cursor1.getInt(0));
        }
        }while(cursor1.moveToNext());
    }
    cursor1.close();

    colorCorrecton();
}

public void enterItem(View v) {
    // TODO Auto-generated method stub
    EditText dataEntry_EditText=(EditText)findViewById(R.id.editText1);
    Editable dataEntry_Editable = dataEntry_EditText.getText();
    String dataEntry_Finaldata = dataEntry_Editable.toString();
    long newId = myDB.insertRow(dataEntry_Finaldata, 233, "red");

    //Toast.makeText(MainActivity.this, dataEntry_Finaldata, Toast.LENGTH_LONG).show();
    dataEntry_EditText.setText("");

    populateListView();

    //Toast.makeText(MainActivity.this, ""+list.getCount(), Toast.LENGTH_LONG).show();
    colorCorrecton();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    closeDB();
}

private void closeDB() {
    // TODO Auto-generated method stub
    myDB.close();
}

private void openDB() {
    // TODO Auto-generated method stub
    myDB = new DBAdapter(this);
    yourDB = new DBAdapter1(this);
    myDB.open();
    yourDB.open();
}

private void populateListView() {
    // TODO Auto-generated method stub

    Cursor cursor = myDB.getAllRows();
    ArrayList<String> myItems = new ArrayList<String>();
    //String[] myItems = {};

    if(cursor.moveToFirst()){
        int countData=0;
        do{
        countData=countData+1;
        int id = cursor.getInt(0);
        String name = "#"+countData +"- "+ cursor.getString(1);
        myItems.add(name);
        }while(cursor.moveToNext());
    }
    cursor.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item,myItems);

    list.setAdapter(adapter);

    //Toast.makeText(MainActivity.this, Integer.toString(list.getFirstVisiblePosition())+","+Integer.toString(list.getLastVisiblePosition()), Toast.LENGTH_LONG).show();
    //View v = list.getChildAt(0);
    //v.setBackgroundColor(005566);
    //View listItemBla=(View)list.getChildAt(list.getFirstVisiblePosition());
    //Toast.makeText(MainActivity.this, ""+ list.getChildCount(), Toast.LENGTH_LONG).show();
    //TextView txtDep=(TextView)listItemBla.findViewById(R.id.tv);
    //txtDep.setBackgroundColor(Color.parseColor("00FF00"));
}

}

logcat的

    06-21 09:46:13.656: D/libEGL(24175): loaded /system/lib/egl/libEGL_adreno200.so
    06-21 09:46:13.656: D/libEGL(24175): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
    06-21 09:46:13.656: D/libEGL(24175): loaded /system/lib/egl/libGLESv2_adreno200.so
    06-21 09:46:13.666: I/Adreno200-EGL(24175): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400_msm8960_JB_2.5_CL3744273_release_AU (CL3744273)
    06-21 09:46:13.666: I/Adreno200-EGL(24175): Build Date: 06/30/13 Sun
    06-21 09:46:13.666: I/Adreno200-EGL(24175): Local Branch: 
    06-21 09:46:13.666: I/Adreno200-EGL(24175): Remote Branch: quic/jb_2.5
    06-21 09:46:13.666: I/Adreno200-EGL(24175): Local Patches: NONE
    06-21 09:46:13.666: I/Adreno200-EGL(24175): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400 +  NOTHING
    06-21 09:46:13.706: D/OpenGLRenderer(24175): Enabling debug mode 0
    06-21 09:46:24.217: D/AndroidRuntime(24175): Shutting down VM
    06-21 09:46:24.217: W/dalvikvm(24175): threadid=1: thread exiting with uncaught exception (group=0x40c12ae0)
    06-21 09:46:24.217: E/AndroidRuntime(24175): FATAL EXCEPTION: main
    06-21 09:46:24.217: E/AndroidRuntime(24175): java.lang.IllegalStateException: Could not execute method of the activity
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.view.View$1.onClick(View.java:3673)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.view.View.performClick(View.java:4278)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.view.View$PerformClick.run(View.java:17429)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.os.Handler.handleCallback(Handler.java:725)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.os.Handler.dispatchMessage(Handler.java:92)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.os.Looper.loop(Looper.java:137)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.app.ActivityThread.main(ActivityThread.java:5099)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at java.lang.reflect.Method.invoke(Method.java:511)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at dalvik.system.NativeStart.main(Native Method)
    06-21 09:46:24.217: E/AndroidRuntime(24175): Caused by: java.lang.reflect.InvocationTargetException
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at java.lang.reflect.Method.invoke(Method.java:511)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at android.view.View$1.onClick(View.java:3668)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    ... 11 more
    06-21 09:46:24.217: E/AndroidRuntime(24175): Caused by: java.lang.NullPointerException
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at com.example.bucketlist.MainActivity.colorCorrecton(MainActivity.java:47)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    at com.example.bucketlist.MainActivity.enterItem(MainActivity.java:204)
    06-21 09:46:24.217: E/AndroidRuntime(24175):    ... 14 more
    06-21 09:46:26.069: I/Process(24175): Sending signal. PID: 24175 SIG: 9

2 个答案:

答案 0 :(得分:2)

问题是,使用

TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv);

告诉系统在listItemBla1内查找ID为tv的视图,它显然无法找到,因为View内不存在ListView (至少不是你期望的那样)。

您需要做的是创建自定义适配器并覆盖getView(),您可以使用所需的TextView来扩充布局。

答案 1 :(得分:0)

你为什么不试着把这一行,

setContentView(R.layout.activity_main);

在你的&#34;颜色校正&#34;功能。

可能它可以解决您的问题。