probléme procedure onCreate()
Bonsoir, j'ai un soucie dans ma procedure des lors que je rajoute du code en dessous du setContentView, mon application plante et aucune erreur null part pour me renseigner
J'ai l'erreur suivante dans mon emulator
the application has stopped unexpectedly android please try again
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| WritableWorkbook wb;
WritableSheet ws;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Filewriter f=new Filewriter();
wb=f.createWorkbook("aa.xls");
ws=f.createSheet(wb, "sheetName", 0);
Toast.makeText(getApplicationContext(), "created", Toast.LENGTH_LONG).show();
try{
f.writeCell(1,1, "success", true, ws);
}catch(RowsExceededException r){} catch(WriteException w){}
} |
Merci les amis, je suis novice en développement android
Caused by: java.lang.NullPointerException
Salut les amis j'ai un probleme de null pointeur exception, peut etre un objet non instancier, mais je ne trouve pas voici mes locCats
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
02-19 10:28:04.373: E/AndroidRuntime(340): FATAL EXCEPTION: main
02-19 10:28:04.373: E/AndroidRuntime(340): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.contact/com.contact.MainActivity}: java.lang.NullPointerException
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.os.Looper.loop(Looper.java:123)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 10:28:04.373: E/AndroidRuntime(340): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 10:28:04.373: E/AndroidRuntime(340): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 10:28:04.373: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 10:28:04.373: E/AndroidRuntime(340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 10:28:04.373: E/AndroidRuntime(340): at dalvik.system.NativeStart.main(Native Method)
02-19 10:28:04.373: E/AndroidRuntime(340): Caused by: java.lang.NullPointerException
02-19 10:28:04.373: E/AndroidRuntime(340): at com.contact.Filewriter.createSheet(Filewriter.java:58)
02-19 10:28:04.373: E/AndroidRuntime(340): at com.contact.MainActivity.onCreate(MainActivity.java:26)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 10:28:04.373: E/AndroidRuntime(340): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-19 10:28:04.373: E/AndroidRuntime(340): ... 11 more |
Voici ma class main entiere
Code:
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
| package com.contact;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
WritableWorkbook wb;
WritableSheet ws;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Filewriter f=new Filewriter();
wb = f.createWorkbook("aa.xls");
ws = f.createSheet(wb, "sheetName", 0);
Toast.makeText(getApplicationContext(), "created", Toast.LENGTH_LONG).show();
try{
f.writeCell(1,1, "success", true, ws);
}catch(RowsExceededException r){} catch(WriteException w){}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/*public void createExcel(){
JavaExcelWrite myExcel = new JavaExcelWrite();
myExcel.createExcel();
}*/
} |
et ma classe que j'instancie afinc de creer mon excel et ca feuille
Code:
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
| package com.contact;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.os.Environment;
import android.util.Log;
public class Filewriter {
public WritableWorkbook createWorkbook(String fileName){
//exports must use a temp file while writing to avoid memory hogging
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
//get the sdcard's directory
File sdCard = Environment.getExternalStorageDirectory();
//add on the app's path
File dir = new File(sdCard.getAbsolutePath() + "/JExcelApiTest");
//make them in case they're not there
dir.mkdirs();
//create a standard java.io.File object for the Workbook to use
File wbfile = new File(dir,fileName);
WritableWorkbook wb = null;
try{
//create a new WritableWorkbook using the java.io.File and
//WorkbookSettings from above
wb = Workbook.createWorkbook(wbfile,wbSettings);
}catch(IOException ex){
Log.e("dd",ex.getStackTrace().toString());
}
return wb;
}
/**
*
* @param wb - WritableWorkbook to create new sheet in
* @param sheetName - name to be given to new sheet
* @param sheetIndex - position in sheet tabs at bottom of workbook
* @return - a new WritableSheet in given WritableWorkbook
*/
public WritableSheet createSheet(WritableWorkbook wb,
String sheetName, int sheetIndex){
//create a new WritableSheet and return it
return wb.createSheet(sheetName, sheetIndex);
}
/**
*
* @param columnPosition - column to place new cell in
* @param rowPosition - row to place new cell in
* @param contents - string value to place in cell
* @param headerCell - whether to give this cell special formatting
* @param sheet - WritableSheet to place cell in
* @throws RowsExceededException - thrown if adding cell exceeds .xls row limit
* @throws WriteException - Idunno, might be thrown
*/
public void writeCell(int columnPosition, int rowPosition, String contents, boolean headerCell,
WritableSheet sheet) throws RowsExceededException, WriteException{
//create a new cell with contents at position
Label newCell = new Label(columnPosition,rowPosition,contents);
if (headerCell){
//give header cells size 10 Arial bolded
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
//center align the cells' contents
headerFormat.setAlignment(Alignment.CENTRE);
newCell.setCellFormat(headerFormat);
}
sheet.addCell(newCell);
}
} |
De plus une petite question, le JRE des composant java sont inclut dans les librairie l'android?
Merci l'ami