Android tells me that The application has stopped unexcepetedly, but there are no error's in my code.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TableLayout;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 6; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
for(int j = 0; j < 6; j++) {
ImageView img = (ImageView) tl.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
Logcat says:
03-03 19:25:43.550: WARN/dalvikvm(487): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): FATAL EXCEPTION: main
03-03 19:25:43.580: ERROR/AndroidRuntime(487): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Looper.loop(Looper.java:123)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invoke(Method.java:507)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at dalvik.system.NativeStart.main(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): Caused by: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): ... 11 more
EDIT: The problem was on the layout:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 7; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
TableRow tr = (TableRow) tl.getChildAt(i);
for(int j = 0; j < 7; j++) {
ImageView img = (ImageView) tr.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
Look at Logcat to see the stack trace which will pinpoint the fault location.
My guess is that findViewById() or getChildAt() is returning null, i.e. your layout isn't quite what you think it is.
TableLayout tl = (TableLayout) findViewById(R.id.T); :
The result of findViewById(R.id.T) is most likely not of type TableLayout (but not null).
Try to print out findViewById(R.id.T).getClass().getName();
That's what this tells me:
Caused by: java.lang.ClassCastException: android.widget.TableRow
at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
I was getting this error for the reason of wrong encoding for layout.xml. Instead of...
<?xml version="1.0" encoding="iso-8859-1"?>
...it should have been in my case:
<?xml version="1.0" encoding="utf-8"?>
Little hard to figure this one out.
Related
I'm trying to insert an image in a sheet using Apache POI, but I'm getting the following error:
03-03 20:21:50.898: E/SELinux(28413): selinux_android_seapp_context_reload: Error reading /seapp_contexts, line 16, name levelFrom, value container
03-03 20:21:50.908: D/dalvikvm(28413): Late-enabling CheckJNI
03-03 20:21:51.668: D/libEGL(28413): loaded /vendor/lib/egl/libEGL_adreno.so
03-03 20:21:51.678: D/libEGL(28413): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
03-03 20:21:51.708: D/libEGL(28413): loaded /vendor/lib/egl/libGLESv2_adreno.so
03-03 20:21:51.718: I/Adreno-EGL(28413): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_3.2_RB3.04.03.00.134.050_msm8610_JB_3.2_RB3__release_AU ()
03-03 20:21:51.718: I/Adreno-EGL(28413): OpenGL ES Shader Compiler Version: 20.00.02
03-03 20:21:51.718: I/Adreno-EGL(28413): Build Date: 11/12/13 Tue
03-03 20:21:51.718: I/Adreno-EGL(28413): Local Branch:
03-03 20:21:51.718: I/Adreno-EGL(28413): Remote Branch: quic/jb_3.2_rb3.21
03-03 20:21:51.718: I/Adreno-EGL(28413): Local Patches: NONE
03-03 20:21:51.718: I/Adreno-EGL(28413): Reconstruct Branch: AU_LINUX_ANDROID_JB_3.2_RB3.04.03.00.134.050 + NOTHING
03-03 20:21:51.978: D/OpenGLRenderer(28413): Enabling debug mode 0
03-03 20:21:52.518: W/IInputConnectionWrapper(28413): getExtractedText on inactive InputConnection
03-03 20:21:52.528: W/IInputConnectionWrapper(28413): getTextBeforeCursor on inactive InputConnection
03-03 20:21:52.538: W/IInputConnectionWrapper(28413): getSelectedText on inactive InputConnection
03-03 20:21:52.538: W/IInputConnectionWrapper(28413): getTextAfterCursor on inactive InputConnection
03-03 20:21:58.468: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.468: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.978: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:21:58.978: E/SpannableStringBuilder(28413): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-03 20:22:02.368: D/dalvikvm(28413): GC_FOR_ALLOC freed 235K, 11% free 9609K/10696K, paused 15ms, total 15ms
03-03 20:22:02.418: D/dalvikvm(28413): GC_FOR_ALLOC freed 11K, 9% free 10061K/10972K, paused 13ms, total 13ms
03-03 20:22:02.458: D/dalvikvm(28413): GC_FOR_ALLOC freed 9K, 8% free 10532K/11424K, paused 15ms, total 15ms
03-03 20:22:02.928: D/dalvikvm(28413): GC_FOR_ALLOC freed 734K, 13% free 11166K/12760K, paused 24ms, total 24ms
03-03 20:22:03.398: W/dalvikvm(28413): Exception Ljava/lang/RuntimeException; thrown while initializing Lorg/apache/poi/ddf/DefaultEscherRecordFactory;
03-03 20:22:03.398: D/AndroidRuntime(28413): Shutting down VM
03-03 20:22:03.398: W/dalvikvm(28413): threadid=1: thread exiting with uncaught exception (group=0x41d23898)
03-03 20:22:03.428: E/AndroidRuntime(28413): FATAL EXCEPTION: main
03-03 20:22:03.428: E/AndroidRuntime(28413): java.lang.IllegalStateException: Could not execute method of the activity
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$1.onClick(View.java:3839)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View.performClick(View.java:4476)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$PerformClick.run(View.java:18795)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Handler.handleCallback(Handler.java:730)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.os.Looper.loop(Looper.java:176)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 20:22:03.428: E/AndroidRuntime(28413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
03-03 20:22:03.428: E/AndroidRuntime(28413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
03-03 20:22:03.428: E/AndroidRuntime(28413): at dalvik.system.NativeStart.main(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.reflect.InvocationTargetException
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 20:22:03.428: E/AndroidRuntime(28413): at android.view.View$1.onClick(View.java:3834)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 11 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.ExceptionInInitializerError
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.AbstractEscherHolderRecord.convertToEscherRecords(AbstractEscherHolderRecord.java:78)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.AbstractEscherHolderRecord.convertRawBytesToEscherRecords(AbstractEscherHolderRecord.java:73)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.record.DrawingGroupRecord.processChildRecords(DrawingGroupRecord.java:79)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.model.InternalWorkbook.findDrawingGroup(InternalWorkbook.java:2051)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.usermodel.HSSFWorkbook.initDrawings(HSSFWorkbook.java:1588)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1608)
03-03 20:22:03.428: E/AndroidRuntime(28413): at pro.kondratev.androidreadxlsx.ReadXlsx.criar(ReadXlsx.java:136)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 14 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: RECORD_ID
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.recordsToMap(DefaultEscherRecordFactory.java:135)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.<clinit>(DefaultEscherRecordFactory.java:42)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 21 more
03-03 20:22:03.428: E/AndroidRuntime(28413): Caused by: java.lang.NoSuchFieldException: RECORD_ID
03-03 20:22:03.428: E/AndroidRuntime(28413): at java.lang.Class.getField(Class.java:673)
03-03 20:22:03.428: E/AndroidRuntime(28413): at org.apache.poi.ddf.DefaultEscherRecordFactory.recordsToMap(DefaultEscherRecordFactory.java:129)
03-03 20:22:03.428: E/AndroidRuntime(28413): ... 22 more
Code is:
public void criar (View view) throws IOException {
String nome = ((String) txtnome.getText().toString());
String foto = ((String) txtstring.getText().toString());
FileInputStream input_document = new FileInputStream(new File("sdcard/projetos/enguelber/formulario.xls"));
HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
HSSFSheet sheet = my_xls_workbook.getSheetAt(0);
// Nome do cliente
Row row = sheet.getRow(7);
Cell cell = row.getCell(1);
String cliente = ((String) txtcliente.getText().toString());
cell.setCellValue(cliente);
// CPF do cliente
Row row1 = sheet.getRow(7);
Cell cell1 = row.getCell(7);
String cpf = ((String) txtcpf.getText().toString());
cell1.setCellValue(cpf);
// Hora
String hora = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss",
Locale.getDefault()).format(new Date());
Row row2 = sheet.getRow(35);
Cell cell2 = row2.getCell(1);
cell2.setCellValue(hora);
// Foto do relatorio
InputStream inputStream = new FileInputStream("sdcard/projetos/enguelber/fotos/02.03.2016 21.15.27.jpg");
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = my_xls_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
inputStream.close();
CreationHelper helper = my_xls_workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
// foto
// Row row3 = sheet.getRow(13);
// Cell cell3 = row3.getCell(1);
// cell3.setCellValue(foto);
input_document.close();
FileOutputStream fileOut = null;
FileOutputStream output_file =new FileOutputStream(new File("sdcard/projetos/enguelber/"+ nome + ".xls"));
my_xls_workbook.write(output_file);
output_file.close();
}
The application works normally if I remove the lines:
InputStream inputStream = new FileInputStream("sdcard/projetos/enguelber/fotos/02.03.2016 21.15.27.jpg");
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = my_xls_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
inputStream.close();
CreationHelper helper = my_xls_workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
Stack trace: http://freetexthost.com/qakvlvuxlm
I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file
I have this code witch returns the content of a file:
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
I'm calling it like that:
System.out.println("FILE CONTENT: " + readFromFile1(this));
and everything runs just purfect.
Now In another activity I'm using almost the same code, but it wont run.
Here it is:
#SuppressLint("NewApi")
public class FavoriteJokes extends Activity {
public static ArrayAdapter<String> adapter;
public static ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_jokes);
final GlobalsHolder globals = (GlobalsHolder)getApplication();
TextView text = (TextView) findViewById(R.id.txtJoke);
text.setText(globals.getList().get(globals.clickedPosition));
ArrayList<String> jokeNamesList = new ArrayList<String>();
jokeNamesList.addAll(readFromFile1(this));
System.out.println("FILE CONTENT: " + readFromFile1(this));
globals.setFavJokeNamesList(readFromFile1(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite_jokes, menu);
return true;
}
/* Read file's content */
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
return list;
}
}
And here is the error output:
03-03 10:50:21.055: E/AndroidRuntime(1038): FATAL EXCEPTION: main
03-03 10:50:21.055: E/AndroidRuntime(1038): Process: com.gs.britishjokes, PID: 1038
03-03 10:50:21.055: E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.britishjokes/com.gs.britishjokes.FavoriteJokes}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Looper.loop(Looper.java:137)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invoke(Method.java:515)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-03 10:50:21.055: E/AndroidRuntime(1038): at dalvik.system.NativeStart.main(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.get(ArrayList.java:308)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.gs.britishjokes.FavoriteJokes.onCreate(FavoriteJokes.java:31)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Activity.performCreate(Activity.java:5243)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-03 10:50:21.055: E/AndroidRuntime(1038): ... 11 more
I really can't understand what is wrong with my code. I'm pretty sure that I'm missing a really small part, but I'm unable to find it. Can you give me a push?
The error log (logcat) indicates an IndexOutOfBoundsException exception: an access to element 0 of an empty array.
It seems to be this line that cause the problem: text.setText(globals.getList().get(globals.clickedPosition));
Maybee you can check the size of globals.getList() ?
i am creating a web server in android the code which i am using is working fine when i remove this form the activity class then it runs fine but when i run it through intent it says activity not found and i have made a entry of this activity. this is my code..
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.security.acl.Owner;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import dolphin.devlopers.com.R;
public class JHTTS extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my Android := "+ownIP.getHostAddress());
Toast.makeText(getApplicationContext(), "Your ip is :: "+ownIP.getHostAddress(), 100).show();
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
try{
File documentRootDirectory = new File ("/sdcard/samer/");
JHTTP j = new JHTTP(documentRootDirectory,9000);
j.start();
Toast.makeText(getApplicationContext(), "Phishing Server Started!!", 5).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Jhttp classs:
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import android.util.Log;
public class JHTTP extends Thread {
private File documentRootDirectory;
private String indexFileName = "index.html";
private ServerSocket server;
private int numThreads = 50;
public JHTTP(File documentRootDirectory, int port,
String indexFileName) throws IOException {
if (!documentRootDirectory.isDirectory( )) {
throw new IOException(documentRootDirectory
+ " does not exist as a directory");
}
this.documentRootDirectory = documentRootDirectory;
this.indexFileName = indexFileName;
this.server = new ServerSocket(port);
}
public JHTTP(File documentRootDirectory, int port) throws IOException {
this(documentRootDirectory, port, "index.html");
}
public JHTTP(File documentRootDirectory) throws IOException {
this(documentRootDirectory, 80, "index.html");
}
public void run( ) {
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(
new RequestProcessor(documentRootDirectory, indexFileName));
t.start( );
}
System.out.println("Accepting connections on port " + server.getLocalPort( ));
System.out.println("Document Root: " + documentRootDirectory);
while (true) {
try {
Socket request = server.accept( );
request.setReuseAddress(true);
RequestProcessor.processRequest(request);
}
catch (IOException ex) {
}
}
}
public static void main(String[] args) {
// get the Document root
File docroot;
try {
docroot = new File("D:/");
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Usage: java JHTTP docroot port indexfile");
return;
}
// set the port to listen on
try {
int port;
port = 9000;
JHTTP webserver = new JHTTP(docroot, port);
webserver.start( );
}
catch (IOException ex) {
System.out.println("Server could not start because of an "
+ ex.getClass( ));
System.out.println(ex);
}
}
}
Logcat:
07-26 21:51:33.447: E/AndroidRuntime(591): FATAL EXCEPTION: main
07-26 21:51:33.447: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{dolphin.devlopers.com/dolphin.developers.com.JHTTS}: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.os.Looper.loop(Looper.java:123)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 21:51:33.447: E/AndroidRuntime(591): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 21:51:33.447: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 21:51:33.447: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-26 21:51:33.447: E/AndroidRuntime(591): at dalvik.system.NativeStart.main(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Activity.startActivityForResult(Activity.java:2817)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Activity.startActivity(Activity.java:2923)
07-26 21:51:33.447: E/AndroidRuntime(591): at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:24)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 21:51:33.447: E/AndroidRuntime(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-26 21:51:33.447: E/AndroidRuntime(591): ... 11 more
Manifest File:
<activity android:name="dolphin.developers.com.JHTTS"></activity>
new Logcat::
07-28 08:58:58.031: W/System.err(1687): java.net.BindException: Permission denied
07-28 08:58:58.031: W/System.err(1687): at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
07-28 08:58:58.040: W/System.err(1687): at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
07-28 08:58:58.050: W/System.err(1687): at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
07-28 08:58:58.060: W/System.err(1687): at java.net.ServerSocket.<init>(ServerSocket.java:138)
07-28 08:58:58.060: W/System.err(1687): at java.net.ServerSocket.<init>(ServerSocket.java:89)
07-28 08:58:58.060: W/System.err(1687): at dolphin.developers.com.JHTTP.<init>(JHTTP.java:28)
07-28 08:58:58.060: W/System.err(1687): at dolphin.developers.com.JHTTP.<init>(JHTTP.java:38)
07-28 08:58:58.070: W/System.err(1687): at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:26)
07-28 08:58:58.070: W/System.err(1687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 08:58:58.070: W/System.err(1687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 08:58:58.070: W/System.err(1687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 08:58:58.081: W/System.err(1687): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 08:58:58.081: W/System.err(1687): at android.os.Looper.loop(Looper.java:123)
07-28 08:58:58.081: W/System.err(1687): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 08:58:58.100: W/System.err(1687): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 08:58:58.100: W/System.err(1687): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 08:58:58.100: W/System.err(1687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 08:58:58.100: W/System.err(1687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 08:58:58.100: W/System.err(1687): at dalvik.system.NativeStart.main(Native Method)
This code
Intent f = new Intent(JHTTS.this, JHTTP.class);
is to start an Activity as if JHHTP was an Activity but it's not, its a Thread inside the Activity. What you need to do is start() the Thread instead of using an Intent
Thread Docs
here are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method, or construct a new Thread and pass a Runnable to the constructor. In either case, the start() method must be called to actually execute the new Thread.
Instead of using Intent try something like
JHTTP jhttp = new JHTTP();
jhttp.start();
I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>