Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Where Eclipse stored Settings and Templates Java Code



397LW NO topic_id


Další témata ....(Topics)


java.lang.NullPointerException: println needs a message | java-lang-nullpointerexception-println-needs-a-message

Problemi is in e.getMessage() what can return null and Log.e (String tag, String msg) will throws an new exception !!!!
Problem and solution:

      	try {
      		int [] i = {1};
      		int z = i[5];
      	} catch (ArrayIndexOutOfBoundsException e) {
      		String s = e.toString(); // s == java.lang.ArrayIndexOutOfBoundsException
                // try to testing String s for null value
      		if(s != null)
      		 Log.e("bla", s);
      		 Log.e("bla", "My error text 1");
      		String s2 = e.getMessage(); // s2 == null !!!!!!!
                // you need to testing String s2 for null value , or you get FATAL EXCEPTION: main
                // and application will be crashed
       		String s2 = e.getMessage(); // s2 == null !!!!!!!
      		if(s2 != null)
      		 Log.e("bla2", e.getMessage());
      		 Log.e("bla2", "My error text 2");
                // this is OK


E/bla(855): java.lang.ArrayIndexOutOfBoundsException

 E/bla2(855): My error text 2

 W/System.err(855): java.lang.ArrayIndexOutOfBoundsException
 W/System.err(855): 	at cz.okhelp.motion._MotionActivity.onTouchEvent(_MotionActivity.java:54)
 W/System.err(855): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
 W/System.err(855): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
 W/System.err(855): 	at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
 W/System.err(855): 	at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
 W/System.err(855): 	at android.os.Handler.dispatchMessage(Handler.java:99)
 W/System.err(855): 	at android.os.Looper.loop(Looper.java:123)
 W/System.err(855): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
 W/System.err(855): 	at java.lang.reflect.Method.invokeNative(Native Method)
 W/System.err(855): 	at java.lang.reflect.Method.invoke(Method.java:507)
 W/System.err(855): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 W/System.err(855): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 W/System.err(855): 	at dalvik.system.NativeStart.main(Native Method)

Or you can using this code:

      	try {
      		int [] i = {1};
      		int z = i[5];
      	} catch (Exception e) {
      		StringBuilder sb = new  StringBuilder().append(e.getClass().getSimpleName()); 
          if  (e.getMessage() != null) { 

"); sb.append(e.getMessage()); } Log.e("err", sb.toString()); // E/err(336): ArrayIndexOutOfBoundsException // this code write out all message Log.e("myError", "methodName", e); } // E/myError(371): methodName // E/myError(371): java.lang.ArrayIndexOutOfBoundsException // E/myError(371): at cz.okhelp.motion._MotionActivity.onTouchEvent(_MotionActivity.java:54) // E/myError(371): at android.app.Activity.dispatchTouchEvent(Activity.java:2099) // E/myError(371): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675) // E/myError(371): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194) // E/myError(371): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878) // E/myError(371): at android.os.Handler.dispatchMessage(Handler.java:99) // E/myError(371): at android.os.Looper.loop(Looper.java:123) // E/myError(371): at android.app.ActivityThread.main(ActivityThread.java:3683) // E/myError(371): at java.lang.reflect.Method.invokeNative(Native Method) // E/myError(371): at java.lang.reflect.Method.invoke(Method.java:507) // E/myError(371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) // E/myError(371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) // E/myError(371): at dalvik.system.NativeStart.main(Native Method)


Delete Bitmap Android Example | delete-bitmap-android-example

If some memory leak - problem try release of memory used of a big Bitmap what already not to need;
Bitmpap bmp; // not null
bmp = null;

final boolean bmpIsRecycled = bmp.isRecycled()
// Returns true if this bitmap has been recycled.

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead”, meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

Write new file or append text Java Android example | write-file-java-android-example

BufferedWriter, FileWriter, write, close MODE_APPEND Java and Android example

Write to file Android example

try {
		String MYFILE = "my_file";
		String strText = "My text";

		// create new file or rewrite existing
                FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_PRIVATE);
                // append to file
                FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_APPEND);

	} catch (IOException e) {

Write to file Java example

try {
// new file    
BufferedWriter out = new BufferedWriter(
    		new FileWriter("outfilename"));
    // append text 
   BufferedWriter out = new BufferedWriter(new FileWriter("outfilename", true));
    out.write("some text");
 } catch (IOException e) {

Eclipse Error parsing the sdk Android | eclipse-error-parsing-the-sdk-android

[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Error parsing the sdk.
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Failed to create C:\Program Files\android\sdk\add-ons.
[2013-07-06 16:10:29 - SDK Manager] [SDK Manager] Error: Unable to parse SDK content.

You have to create add-ons folder as admin of PC

AlertDialog yes no - Alert - Messagebox - Android sample | alertdialog-yes-no-alert-messagebox-android-sample

Dialog Yes No sample code

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit application?")
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish(); //Close  this Activity for example: MyActivity.java
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                // some code if you want
AlertDialog alert = builder.create();

Editace: 2014-04-03 12:45:26
Počet článků v kategorii: 397

11 Share