I'm trying to follow the advice from this post:
But when I run this code, my program crashes. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Change the font for the password hint to match that of the
* user name field */
EditText pw = (EditText) findViewById(R.id.password);
pw.setTypeface(Typeface.DEFAULT); //<---Crashes on this line
pw.setTransformationMethod(new PasswordTransformationMethod());
setContentView(R.layout.activity_main);
}
Here is the XML for this object:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/userName"
android:layout_alignRight="#+id/appName"
android:layout_below="#+id/userName"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword" />
And finally, here is the LogCat output where I can see the error:
05-09 00:30:21.408: E/Trace(16987): error opening trace file: No such
file or directory (2) 05-09 00:30:21.918: D/AndroidRuntime(16987):
Shutting down VM 05-09 00:30:21.918: W/dalvikvm(16987): threadid=1:
thread exiting with uncaught exception (group=0x40a71930) 05-09
00:30:21.978: E/AndroidRuntime(16987): FATAL EXCEPTION: main 05-09
00:30:21.978: E/AndroidRuntime(16987): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.mobilenicity.gen_co_event_marketing_app/com.mobilenicity.gen_co_event_marketing_app.MainActivity}:
java.lang.NullPointerException 05-09 00:30:21.978:
E/AndroidRuntime(16987): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
android.app.ActivityThread.access$600(ActivityThread.java:141) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
android.os.Handler.dispatchMessage(Handler.java:99) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
android.os.Looper.loop(Looper.java:137) 05-09 00:30:21.978:
E/AndroidRuntime(16987): at
android.app.ActivityThread.main(ActivityThread.java:5041) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
java.lang.reflect.Method.invokeNative(Native Method) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
java.lang.reflect.Method.invoke(Method.java:511) 05-09 00:30:21.978:
E/AndroidRuntime(16987): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
dalvik.system.NativeStart.main(Native Method) 05-09 00:30:21.978:
E/AndroidRuntime(16987): Caused by: java.lang.NullPointerException
05-09 00:30:21.978: E/AndroidRuntime(16987): at
com.mobilenicity.gen_co_event_marketing_app.MainActivity.onCreate(MainActivity.java:26)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
android.app.Activity.performCreate(Activity.java:5104) 05-09
00:30:21.978: E/AndroidRuntime(16987): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 00:30:21.978: E/AndroidRuntime(16987): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 00:30:21.978: E/AndroidRuntime(16987): ... 11 more
Can't figure out what is going wrong. Can anybody see it? Thanks!
Change the call of setContentView(R.layout.activity_main);
Call it just after super.onCreate(savedInstanceState);
When you're doing EditText pw = (EditText) findViewById(R.id.password);, findViewById "can't retrieve" the EditText that you have defined in your layout (because you call setContentView after) so findViewById returns null and when you're doing pw.setTypeface(Typeface.DEFAULT); it throws a NPE because pw is null.
Related
The code in my book created a NPE for me to see. But, when I open the logcat in ddms, I cannot see all the errors (it says 11 more). How do I see all the exceptions ? Please see the photo below -
EDIT - Image for full trace posted. Also posting text of trace by copy pasting.
Image -
D/QuizActivity(836): onCreate(Bundle) called
D/AndroidRuntime(836): Shutting down VM
W/dalvikvm(836): threadid=1: thread exiting with uncaught exception (group=0x41465700)
E/AndroidRuntime(836): FATAL EXCEPTION: main
E/AndroidRuntime(836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime(836): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime(836): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(836): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime(836): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(836): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(836): at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(836): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(836): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(836): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(836): Caused by: java.lang.NullPointerException
E/AndroidRuntime(836): at com.bignerdranch.android.geoquiz.QuizActivity.updateQuestion(QuizActivity.java:47)
E/AndroidRuntime(836): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:111)
E/AndroidRuntime(836): at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime(836): ... 11 more
You dont usually need the whole stack trace, the main thing to look for is "caused by" message and the class(TAG) which is causing the exception
Use try catch block and Java Doc
printStackTrace to see the full stack trace.
try {
// Expected Exception Rasing Line
} catch (Exception e) {
e.printStackTrace();
}
Hi I have an activity that shows a dialog form an standard layout.
android:theme="#android:style/Theme.Holo.Light.Dialog"/>
I was trying to add a simple spinner to the actionbar but I am having issues and error in the line getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST).
When I change my theme to a common activity it works perfect.
This are the errors showed by the logcat:
3114-3114/org.PeteProto E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.PeteProto/org.PeteProto.Detail}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.PeteProto.Detail.onCreate(Detail.java:30)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
I am trying to view a file word as well as .txt file using android as follows:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file:///android_asset/usage.txt"), "plain/text");
getApplicationContext().startActivity(intent);
and
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file:///android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc"), "plain/text");
getApplicationContext().startActivity(intent);
inside a method of an activity. Now, i have ES File Manager installed on my device. But i keep on getting the error:
05-09 01:24:53.516: W/System.err(28918): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc typ=plain/text flg=0x10000000 }
05-09 01:24:53.516: W/System.err(28918): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
05-09 01:24:53.516: W/System.err(28918): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
05-09 01:24:53.516: W/System.err(28918): at android.app.ContextImpl.startActivity(ContextImpl.java:949)
05-09 01:24:53.524: W/System.err(28918): at android.app.ContextImpl.startActivity(ContextImpl.java:931)
05-09 01:24:53.524: W/System.err(28918): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
05-09 01:24:53.524: W/System.err(28918): at com.pack.android.activity.ResponderActivity.onHelpMenuSelected(ResponderActivity.java:1733)
05-09 01:24:53.524: W/System.err(28918): at com.pack.android.activity.ResponderActivity.onOptionsItemSelected(ResponderActivity.java:1711)
05-09 01:24:53.532: W/System.err(28918): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
05-09 01:24:53.532: W/System.err(28918): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
05-09 01:24:53.532: W/System.err(28918): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-09 01:24:53.532: W/System.err(28918): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-09 01:24:53.539: W/System.err(28918): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-09 01:24:53.539: W/System.err(28918): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
05-09 01:24:53.539: W/System.err(28918): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-09 01:24:53.539: W/System.err(28918): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-09 01:24:53.547: W/System.err(28918): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
05-09 01:24:53.547: W/System.err(28918): at android.widget.AbsListView$1.run(AbsListView.java:3423)
05-09 01:24:53.547: W/System.err(28918): at android.os.Handler.handleCallback(Handler.java:725)
05-09 01:24:53.547: W/System.err(28918): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 01:24:53.547: W/System.err(28918): at android.os.Looper.loop(Looper.java:137)
05-09 01:24:53.555: W/System.err(28918): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 01:24:53.555: W/System.err(28918): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 01:24:53.555: W/System.err(28918): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 01:24:53.555: W/System.err(28918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 01:24:53.555: W/System.err(28918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 01:24:53.563: W/System.err(28918): at dalvik.system.NativeStart.main(Native Method)
and
05-09 01:29:43.071: W/System.err(28918): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///android_asset/usage.txt typ=plain/text flg=0x10000000 }
05-09 01:29:43.071: W/System.err(28918): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
05-09 01:29:43.078: W/System.err(28918): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
05-09 01:29:43.078: W/System.err(28918): at android.app.ContextImpl.startActivity(ContextImpl.java:949)
05-09 01:29:43.086: W/System.err(28918): at android.app.ContextImpl.startActivity(ContextImpl.java:931)
05-09 01:29:43.086: W/System.err(28918): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
05-09 01:29:43.086: W/System.err(28918): at com.pack.android.activity.ResponderActivity.onHelpMenuSelected(ResponderActivity.java:1748)
05-09 01:29:43.086: W/System.err(28918): at com.pack.android.activity.ResponderActivity.onOptionsItemSelected(ResponderActivity.java:1711)
05-09 01:29:43.094: W/System.err(28918): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
05-09 01:29:43.094: W/System.err(28918): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
05-09 01:29:43.094: W/System.err(28918): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-09 01:29:43.094: W/System.err(28918): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-09 01:29:43.094: W/System.err(28918): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-09 01:29:43.102: W/System.err(28918): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
05-09 01:29:43.102: W/System.err(28918): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-09 01:29:43.102: W/System.err(28918): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-09 01:29:43.102: W/System.err(28918): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
05-09 01:29:43.110: W/System.err(28918): at android.widget.AbsListView$1.run(AbsListView.java:3423)
05-09 01:29:43.110: W/System.err(28918): at android.os.Handler.handleCallback(Handler.java:725)
05-09 01:29:43.110: W/System.err(28918): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 01:29:43.118: W/System.err(28918): at android.os.Looper.loop(Looper.java:137)
05-09 01:29:43.118: W/System.err(28918): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 01:29:43.118: W/System.err(28918): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 01:29:43.118: W/System.err(28918): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 01:29:43.125: W/System.err(28918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 01:29:43.125: W/System.err(28918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 01:29:43.125: W/System.err(28918): at dalvik.system.NativeStart.main(Native Method)
I have got these 2 files in my assets folder but still can't open it!
Any help would be great !
EDIT
using proper MIME_TYPE does resolve the error but now i am getting another error :
05-09 01:42:27.647: W/System.err(29614): java.lang.RuntimeException: java.io.FileNotFoundException: /android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc: open failed: ENOENT (No such file or directory)
05-09 01:42:27.647: W/System.err(29614): at com.tf.fastole2.CompoundBinaryUtil.openRandomAccessFile(CompoundBinaryUtil.java:15)
05-09 01:42:27.647: W/System.err(29614): at com.tf.fastole2.TFOleFileSystem.openFileSystem(TFOleFileSystem.java:81)
05-09 01:42:27.647: W/System.err(29614): at com.tf.fastole2.TFOleFsFactory.openFileSystem(TFOleFsFactory.java:32)
05-09 01:42:27.647: W/System.err(29614): at com.tf.write.filter.CheckFileType.isHwp50(CheckFileType.java:240)
05-09 01:42:27.655: W/System.err(29614): at com.tf.write.filter.CheckFileType.getType(CheckFileType.java:35)
05-09 01:42:27.655: W/System.err(29614): at com.tf.thinkdroid.write.viewer.action.Open$2.run(Unknown Source)
05-09 01:42:27.655: W/System.err(29614): at java.lang.Thread.run(Thread.java:856)
05-09 01:42:27.655: W/System.err(29614): Caused by: java.io.FileNotFoundException: /android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc: open failed: ENOENT (No such file or directory)
05-09 01:42:27.655: W/System.err(29614): at libcore.io.IoBridge.open(IoBridge.java:416)
05-09 01:42:27.655: W/System.err(29614): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:118)
05-09 01:42:27.655: W/System.err(29614): at com.tf.fastole2.CompoundBinaryUtil.openRandomAccessFile(CompoundBinaryUtil.java:11)
05-09 01:42:27.655: W/System.err(29614): ... 6 more
05-09 01:42:27.655: W/System.err(29614): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-09 01:42:27.655: W/System.err(29614): at libcore.io.Posix.open(Native Method)
05-09 01:42:27.655: W/System.err(29614): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-09 01:42:27.655: W/System.err(29614): at libcore.io.IoBridge.open(IoBridge.java:400)
05-09 01:42:27.655: W/System.err(29614): ... 8 more
05-09 01:42:27.655: W/System.err(29614): java.io.FileNotFoundException: /android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc: open failed: ENOENT (No such file or directory)
05-09 01:42:27.655: W/System.err(29614): at libcore.io.IoBridge.open(IoBridge.java:416)
05-09 01:42:27.655: W/System.err(29614): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:118)
05-09 01:42:27.655: W/System.err(29614): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:150)
05-09 01:42:27.655: W/System.err(29614): at java.util.zip.ZipFile.<init>(ZipFile.java:130)
05-09 01:42:27.655: W/System.err(29614): at java.util.zip.ZipFile.<init>(ZipFile.java:103)
05-09 01:42:27.655: W/System.err(29614): at com.tf.io.CachedZipFile$RealZipFileImpl.<init>(CachedZipFile.java:274)
05-09 01:42:27.655: W/System.err(29614): at com.tf.io.CachedZipFile$RealZipFileImpl.<init>(CachedZipFile.java:269)
05-09 01:42:27.655: W/System.err(29614): at com.tf.io.CachedZipFile.create(CachedZipFile.java:61)
05-09 01:42:27.655: W/System.err(29614): at com.tf.io.CachedZipFile.create(CachedZipFile.java:37)
05-09 01:42:27.655: W/System.err(29614): at com.tf.write.filter.CheckFileType.isDocx(CheckFileType.java:322)
05-09 01:42:27.655: W/System.err(29614): at com.tf.write.filter.CheckFileType.getType(CheckFileType.java:47)
05-09 01:42:27.655: W/System.err(29614): at com.tf.thinkdroid.write.viewer.action.Open$2.run(Unknown Source)
05-09 01:42:27.655: W/System.err(29614): at java.lang.Thread.run(Thread.java:856)
05-09 01:42:27.655: W/System.err(29614): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-09 01:42:27.663: W/System.err(29614): at libcore.io.Posix.open(Native Method)
05-09 01:42:27.663: W/System.err(29614): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-09 01:42:27.663: W/System.err(29614): at libcore.io.IoBridge.open(IoBridge.java:400)
05-09 01:42:27.663: W/System.err(29614): ... 12 more
The issue is that you are trying to give access via intents to 3rd party applications that doesn't have permission to access your files.
Android's Security Model doesn't allow any other app to access your private data (assets/resources ....) root here is an exception.
So to make your file available to such apps consider copying your files to a public directory CacheDir maybe the most suitable option.
by doing so you can use intents to view your data the way you are using them right now.
for more about copying files to external dir look here:
Android: how to copy files in assets to sdcard
Also while at it take a look at Android's Security Model
P.S don't forget about WRITE_EXTERNAL_STORAGE permession..
Good luck
You should use text/plain instead of plain/text.
As is mentioned in the exception log,
java.io.FileNotFoundException: /android_asset/USAGE_INSTRUCTIONS_FOR_SMART_RESPONDER.doc: open failed: ENOENT (No such file or directory)
its not able to find the file. Verify if the file is indeed there in that location. Also, as Brigham mentioned, use text/plain.
Exception says that File not exist. Watch that you are pointing to /android_asset/ folder in internal memory.
Are you sure that you not put this file on SDCard?
App crashes when i press power button to lock screen and press it again to unlock screen.
App crashes after scren gets unlocked.
This is a stackTress i got in logcat:
05-09 18:46:57.254: E/AndroidRuntime(25354): FATAL EXCEPTION: main
05-09 18:46:57.254: E/AndroidRuntime(25354): java.lang.NullPointerException
05-09 18:46:57.254: E/AndroidRuntime(25354): at org.andengine.ui.activity.BaseGameActivity.onResumeGame(BaseGameActivity.java:222)
05-09 18:46:57.254: E/AndroidRuntime(25354): at org.andengine.ui.activity.BaseGameActivity$4.run(BaseGameActivity.java:373)
05-09 18:46:57.254: E/AndroidRuntime(25354): at android.os.Handler.handleCallback(Handler.java:615)
05-09 18:46:57.254: E/AndroidRuntime(25354): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 18:46:57.254: E/AndroidRuntime(25354): at android.os.Looper.loop(Looper.java:137)
05-09 18:46:57.254: E/AndroidRuntime(25354): at android.app.ActivityThread.main(ActivityThread.java:4744)
05-09 18:46:57.254: E/AndroidRuntime(25354): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 18:46:57.254: E/AndroidRuntime(25354): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 18:46:57.254: E/AndroidRuntime(25354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-09 18:46:57.254: E/AndroidRuntime(25354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-09 18:46:57.254: E/AndroidRuntime(25354): at dalvik.system.NativeStart.main(Native Method)
Nullpointer Exception at onResumeGame() method of BaseGameActivityClass.
I have added this line in "activity" tag of manifest
android:configChanges="orientation|keyboard|keyboardHidden"
in my GameActivity which extends BaseGameActivity i have added this
#Override
public void onPause() {
super.onPause();
mEngine.stop();
}
#Override
protected void onResume() {
// The activity has become visible (it is now "resumed").
super.onResume();
mEngine.start();
}
What is wrong?
What shall i do to stop crashing game on power button issue?
Whenever onResume() method is called it again recreate all your resources.
So at the time of your onResume() method call the controller does not found mEngine instance. Because of this it fires an error NullPointerException.
The task of onPause() method is to stop the music and sound and onResume() method is to again play sound and music. These methods are not used for any other purpose in android game development particularly in AndEngine.
Very short app, you press a button and play a sound bite. The sounds are in mp3/wav format, the app works on the VM in eclipse but does not when trying it on phone (Galaxy S2).
public class SoundBites extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soundbite);
}
public void SheldonButtonListener(View v){
MediaPlayer mp_bazinga = MediaPlayer.create(getBaseContext(), R.raw.bazinga);
mp_bazinga.start();
}
public void SpanishButtonListener(View v){
MediaPlayer mp_spanish = MediaPlayer.create(getBaseContext(), R.raw.spanish_inquisition);
mp_spanish.start();
}
public void NudgeButtonListener(View v){
MediaPlayer mp_nudge = MediaPlayer.create(getBaseContext(), R.raw.nudge_nudge);
mp_nudge.start();
}
}
with xml-file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1.0">
<View
android:id="#+id/buttons_helper1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<View
android:id="#+id/buttons_helper2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<Button
android:id="#+id/bazinga_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="#+id/buttons_helper1"
android:layout_above="#+id/buttons_helper2"
android:background="#drawable/sheldon_cooper"
android:onClick="SheldonButtonListener" />
<Button
android:id="#+id/spanish_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/buttons_helper1"
android:layout_above="#+id/buttons_helper2"
android:background="#drawable/spanish_inquisition"
android:onClick="SpanishButtonListener" />
<Button
android:id="#+id/nudge_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttons_helper2"
android:background="#drawable/nudge_nudge"
android:onClick="NudgeButtonListener" />
</RelativeLayout>
and I get the following error:
05-09 20:12:15.395: E/MediaPlayer(15110): Unable to to create media player
05-09 20:12:15.405: D/MediaPlayer(15110): create failed:
05-09 20:12:15.405: D/MediaPlayer(15110): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-09 20:12:15.405: D/MediaPlayer(15110): at android.media.MediaPlayer.setDataSource(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.media.MediaPlayer.create(MediaPlayer.java:741)
05-09 20:12:15.405: D/MediaPlayer(15110): at com.example.goran.SoundBites.NudgeButtonListener(SoundBites.java:30)
05-09 20:12:15.405: D/MediaPlayer(15110): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.view.View$1.onClick(View.java:3064)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.view.View.performClick(View.java:3591)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.view.View$PerformClick.run(View.java:14263)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.os.Handler.handleCallback(Handler.java:605)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.os.Looper.loop(Looper.java:137)
05-09 20:12:15.405: D/MediaPlayer(15110): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-09 20:12:15.405: D/MediaPlayer(15110): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.405: D/MediaPlayer(15110): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.405: D/MediaPlayer(15110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-09 20:12:15.405: D/MediaPlayer(15110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-09 20:12:15.405: D/MediaPlayer(15110): at dalvik.system.NativeStart.main(Native Method)
05-09 20:12:15.405: D/AndroidRuntime(15110): Shutting down VM
05-09 20:12:15.405: W/dalvikvm(15110): threadid=1: thread exiting with uncaught exception (group=0x40c3e1f8)
05-09 20:12:15.420: E/AndroidRuntime(15110): FATAL EXCEPTION: main
05-09 20:12:15.420: E/AndroidRuntime(15110): java.lang.IllegalStateException: Could not execute method of the activity
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.view.View$1.onClick(View.java:3069)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.view.View.performClick(View.java:3591)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.view.View$PerformClick.run(View.java:14263)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.os.Handler.handleCallback(Handler.java:605)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.os.Looper.loop(Looper.java:137)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-09 20:12:15.420: E/AndroidRuntime(15110): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.420: E/AndroidRuntime(15110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-09 20:12:15.420: E/AndroidRuntime(15110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-09 20:12:15.420: E/AndroidRuntime(15110): at dalvik.system.NativeStart.main(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110): Caused by: java.lang.reflect.InvocationTargetException
05-09 20:12:15.420: E/AndroidRuntime(15110): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:12:15.420: E/AndroidRuntime(15110): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 20:12:15.420: E/AndroidRuntime(15110): at android.view.View$1.onClick(View.java:3064)
05-09 20:12:15.420: E/AndroidRuntime(15110): ... 11 more
05-09 20:12:15.420: E/AndroidRuntime(15110): Caused by: java.lang.NullPointerException
05-09 20:12:15.420: E/AndroidRuntime(15110): at com.example.goran.SoundBites.NudgeButtonListener(SoundBites.java:31)
05-09 20:12:15.420: E/AndroidRuntime(15110): ... 14 more
Tried to google this badboy but have come up empty handed so far, anyone got any ideas? The thing that perturbs me is that it works on the VM but not on my phone.
EDIT: Found two problems, one was that I needed to take control over the Audio Volume, the other one seems to be that my WAV-files are corrupt, made it output the mp3 now at least!
According to this question: R.raw.anything cannot be resolved
Eclipse decided to import android.R not your package_name.R file...
Or
You need to use your full package name to reference raw resources.
MediaPlayer mp_nudge = MediaPlayer.create(getBaseContext(), com.exmaple.goran.R.raw.nudge_nudge);
Try Fix Project Properties. Then cleaning your project!
R wil be generated again.