0

So I have a ListView in my activity_main.xml. Also a TextView in a different xml da_item.xml. The textview is for the individual listview items.

Now in my code there is a onContextItemSelected() function which calls bucketMarkItem(long itemTitle). bucketMarkItem(long itemTitle) changes the last item's background color using function colorCorrection().

The whole code works fine.

But I have a button with enterItem() onClick listener. I call the same colorCorrection() from inside enterItem() and the app crashes. It crashes on this line.

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

And I have no idea why.

I've seen many question like this and the answer was to use setContentView(R.layout.activity_main) but I've already included this.

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() Code

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 Code

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() Code

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();
}

Complete Code

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
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
MetalMonkey
  • 17
  • 1
  • 9

2 Answers2

2

The problem is, using

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

is telling the system to look inside of listItemBla1 for a View with an id of tv which it obviously won't find because no View can exist inside your ListView (at least not how you expect it to).

What you need to do is create a custom adapter and override getView() where you can inflate the layout with the TextView you want.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • his code in adapter seems to be using layout R.layout.da_item so I think he doesnot need getView – Illegal Argument Jun 21 '14 at 04:09
  • 2
    @IllegalArgument true but if he wants to access a View in that layout he needs to. Because that View doesn't exist within the ListView but within the layout inflated by the adapter...unless I missed something. – codeMagic Jun 21 '14 at 04:13
  • @codeMagic : Please note that I have two functions, **enterItem()** and **bucketMarkItem()**. both function call the function **colorCorrection()** which has the **TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv);** command. The app only crashes when I call **colorCorrection()** via **enterItem()**. With **bucketMarkItem()** it works fine. – MetalMonkey Jun 21 '14 at 04:35
  • It worked. I created a custom adapter and used getview. Thanks – MetalMonkey Jun 21 '14 at 13:54
0

Why dont you try to put this line,

setContentView(R.layout.activity_main);

in your "colorCorrection" function .

May be it can solve your problem.

Ishaan Narula
  • 133
  • 1
  • 6