App crashes on findViewById()
本问题已经有最佳答案,请猛点这里访问。
所以我在activity_main.xml中有一个listview。另一个是另一个xml da_item.xml中的文本视图。textView用于单个listView项。
现在在我的代码中有一个onContextItemSelected()函数,它调用bucketmarkItem(long itemtitle)。bucketmarkitem(long itemtitle)使用函数colorcorrection()更改最后一个项的背景色。
整个代码工作正常。
但是我有一个按钮,上面有entertitem()onclick listener。我从EnterItem()内部调用相同的colorcorrection(),应用程序崩溃。它在这条线上崩溃了。
1 | TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv); |
我不知道为什么。
我看到过许多这样的问题,答案是使用setContentView(r.layout.activityu main),但我已经包含了这个问题。
OnContextItemSelected代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @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()代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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(); } |
颜色校正代码
1 2 3 4 5 6 7 8 9 10 | 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()代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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(); } |
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 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")); } } |
洛卡特
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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 |
冰的问题,采用
1 | TextView txtDep1=(TextView)listItemBla1.findViewById(R.id.tv); |
冰告诉系统在看《A View of
你所需要做的是创建一个自定义适配器和
你为什么不试着把这两条线,
1 | setContentView(R.layout.activity_main); |
在你的"colorcorrection"功能。
它可以练习,可能是你的问题。