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
Related
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() ?
Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.
I have a listView inside a fragment.
Im getting some json from the net and passing it into a List<List<String>>.
For some reason the list doesnt work.. there's no crash and there's nothing suspicious in the logcat..
it just doesnt show anything in the ListView.
Adapter's call (From the fragment):
setListAdapter(new MobileArrayAdapter(getActivity(), items));
This is my adapter's code:
package com.example.free;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
public List<List<String>> items;
//private final static String[] a={"s"};
public MobileArrayAdapter(Context context, List<List<String>> items) {
super(context, R.layout.list_item);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
TextView views = (TextView) rowView.findViewById(R.id.views);
TextView likes=(TextView) rowView.findViewById(R.id.likes);
TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
TextView desc=(TextView) rowView.findViewById(R.id.desc);
TextView pub =(TextView) rowView.findViewById(R.id.pub);
TextView idnum =(TextView) rowView.findViewById(R.id.idnum);
Log.e("title",items.get(1).get(1));
title.setText(" "+items.get(1).get(position));
views.setText(items.get(6).get(position));
likes.setText(items.get(7).get(position));
upvote.setText(items.get(8).get(position));
downvote.setText(items.get(9).get(position));
desc.setText(items.get(4).get(position));
pub.setText(items.get(5).get(position));
idnum.setText(items.get(0).get(position));
return rowView;
}
}
items comes full.. I checked it, but for some reason the data doesnt get to the adapter..
Whats wrong here?
EDIT:
10-12 14:23:02.880: W/dalvikvm(23285): threadid=1: thread exiting with uncaught exception (group=0x4107b930)
10-12 14:23:02.900: E/AndroidRuntime(23285): FATAL EXCEPTION: main
10-12 14:23:02.900: E/AndroidRuntime(23285): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.util.ArrayList.get(ArrayList.java:304)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.example.free.MobileArrayAdapter.getView(MobileArrayAdapter.java:49)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.ListView.onMeasure(ListView.java:1158)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2438)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Handler.handleCallback(Handler.java:725)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Looper.loop(Looper.java:137)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285): at dalvik.system.NativeStart.main(Native Method)
EDIT #2:
{List of lists:
{Titles list(Strings):bla,bla,bla},
{desc list(Strings): bla, bla,bla},
{another list(Strings): bla, bla,bla},
}
here's a pic
image
You are chaining to the wrong constructor, and your class is mis-declared.
Change:
public class MobileArrayAdapter extends ArrayAdapter<String>
to:
public class MobileArrayAdapter extends ArrayAdapter<List<String>>
And change:
super(context, R.layout.list_item);
to:
super(context, R.layout.list_item, 0, items);
You can then dump your own items data member and adjust getView() accordingly.
What is happening is that ArrayAdapter knows nothing of your List<String>, and so therefore it will return getCount() of 0.
The 0 in the super() call here is safe because you are not chaining to the superclass, electing to handle everything in getView() yourself.
However, that points out issues in your getView() implementation:
Please recycle your rows, by using convertView if it is not null
Consider using getActivity().getLayoutInflater() instead of always creating a new one, in case the activity either caches a LayoutInflater or supplies you with a specialized one
" "+items.get(1).get(position) is probably not a good idea, compared to designing your row layout such that this TextView is positioned where you want it to be
Given how many widgets are in your row, please consider using the ViewHolder pattern to cache those findViewById() calls
Please check below code if it is working
I think Your Items list has no more data which you rae going to access
Check Below code:
title.setText(" "+items.get(position).get(0));
views.setText(items.get(position).get(1));
likes.setText(items.get(position).get(2));
upvote.setText(items.get(position).get(3));
downvote.setText(items.get(position).get(4));
desc.setText(items.get(position).get(5));
pub.setText(items.get(position).get(6));
idnum.setText(items.get(position).get(7));
May be list size is small and returns null so check this one.
I am developing an application in android and I have a requirement to read and write from an excel file stored in sdcard.I am using jxl library for this purpose. But I am getting some errors with file operation.
ext = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hai/dynamic.xls";
setOutputFile(ext);
try {
write();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setOutputFile(String inputFile) {
this.inputfile = inputFile;
}
public void write() throws IOException, WriteException {
File file = new File(inputfile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
Then I am getting this following error in logcat
01-09 12:43:12.824: W/System.err(12321): java.io.FileNotFoundException:/mnt/sdcard/hai/dynamic.xls (Permission denied)
01-09 12:43:12.884: W/System.err(12321): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-09 12:43:12.884: W/System.err(12321): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
01-09 12:43:12.904: W/System.err(12321): at jxl.Workbook.createWorkbook(Workbook.java:301)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.write(WriteAcivity.java:59)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.onCreate(WriteAcivity.java:40)
01-09 12:43:12.904: W/System.err(12321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 12:43:12.904: W/System.err(12321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-09 12:43:12.924: W/System.err(12321): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:43:12.924: W/System.err(12321): at android.os.Looper.loop(Looper.java:123)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 12:43:12.924: W/System.err(12321): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:43:12.944: W/System.err(12321): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 12:43:12.944: W/System.err(12321): at dalvik.system.NativeStart.main(Native Method)
//add use permission in your manifest
WRITE_EXTERNAL_STORAGE
Allows an application to write to external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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.