error with the map Activity - java

I develop a simple mapActivity that aska the user to check if he has enabled the gps system or not by using the dialog alert and the intent for the location service. And if the gps is enables it goes direct to the mapView but this do not happen with me. It makes me force close when I press to go to the mapView.
This is the dialog code:
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes TO ENABLE GPS")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 1);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
//dialog.cancel();
Intent intent = new Intent(MainActivity.this, AndroidGoogleMapsActivity.class);
context.startActivity(intent);
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
And this is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" ><uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AndroidGoogleMapsActivity" >
</activity>
</application>
</manifest>
this is the code of the AndroidGoogleMapsActivity class
public class AndroidGoogleMapsActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Displaying Zooming controls
MapView mapView = (MapView) findViewById(R.id.mapView);
Log.d("error", "the negative button befor the line 27 ");
mapView.setBuiltInZoomControls(true);
Log.d("error", "the negative button after the line 27 befor the map view settings ");
mapView.setSatellite(true); // Satellite View
Log.d("error", "the negative button after the line 27 after satellite view ");
mapView.setStreetView(false); // Street View
Log.d("error", "the negative button after the line 27 after street view ");
mapView.setTraffic(true); // Traffic view
Log.d("error", "the negative button after the line 27 after the map view settings ");
MapController mc = mapView.getController();
Log.d("error", "the negative button after the line 27 after the mapController ");
double lat = Double.parseDouble("33.823862");// kehale
double lon = Double.parseDouble("35.590248");// kehale
GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
mc.animateTo(geoPoint);
mc.setZoom(15);
mapView.invalidate();
Log.d("error", "the negative button befor the GeoPoint");
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
And this is the error that I get:
09-20 05:16:22.512: D/AndroidRuntime(309): Shutting down VM
09-20 05:16:22.512: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-20 05:16:22.543: E/AndroidRuntime(309): FATAL EXCEPTION: main
09-20 05:16:22.543: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.AndroidGoogleMapsActivity}: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-20 05:16:22.543: E/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521)
09-20 05:16:22.543: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-20 05:16:22.543: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-20 05:16:22.543: E/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309): at com.mkyong.android.AndroidGoogleMapsActivity.onCreate(AndroidGoogleMapsActivity.java:27)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-20 05:16:22.543: E/AndroidRuntime(309): ... 11 more
09-20 05:16:25.141: I/Process(309): Sending signal. PID: 309 SIG: 9

Seems like you got a problem in your layout-file that causes this NullPointerException.
// This command tries to get the mapView but fails to find it, so it returns null, instead of the mapView
MapView mapView = (MapView) findViewById(R.id.mapView);
// This tries to call a method on null, which will cause a NullPointerException.
mapView.setBuiltInZoomControls(true);
To solve this check that in your layout/map_activity.xml the map is actually named mapView like this:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />

Related

nullpointerexception at Action bar setDisplayShowHomeEnabled () in Android

I create custom action bar I really don't know where is my mistake.I have some problems while creating Custom action bar.
And showing the null pointer exception at the line = mActionBar.setDisplayShowHomeEnabled(false);
Here is my error log
07-29 16:19:20.180 32714-32714/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tazeen.classnkk/com.example.tazeen.classnkk.AllPosts_Page}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tazeen.classnkk.AllPosts_Page.CustomActionBar(AllPosts_Page.java:37)
at com.example.tazeen.classnkk.AllPosts_Page.onCreate(AllPosts_Page.java:28)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            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:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
I have try last 5 to 7 hours , but can not find any proper solution.Please help me.Thanks.
Here is my Activity code
public class AllPosts_Page extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_post);
CustomActionBar();
}
public void CustomActionBar()
{
android.app.ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_action_bar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.txtTitle);
mTitleTextView.setText("All Post");
ImageView imageButton = (ImageView) mCustomView
.findViewById(R.id.imgLeftMenu);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Refresh Clicked!",
Toast.LENGTH_LONG).show();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
menifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<activity
android:name=".Splash_Screen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login_Screen" />
<activity
android:name=".AllPosts_Page">
</activity>
<activity
android:name=".Filter_Page"
android:label="#string/title_activity_filter__page"
android:theme="#style/ListFont">
</activity>
</application>
</manifest>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:height">5dp</item>
</style>
</resources>
you are using Theme.AppCompat.Light.DarkActionBar so you should get your ActionBar by getSupportActionBar() method, not getActionBar()
if your extended Activity is from Android system then style is incorrect (AppCompat) and Activity gets some default styling without ActionBar, so getActionBar() return null
else if you want to use this AppCompat style it is applicable for AppCompatActivity (and then you use should getSupportActionBar())

Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml

I'm trying unzip some files in background, so I use IntentService like in google's tutorial. My service class declared in AndroidManifest like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.osmdroid">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:configChanges="orientation|screenSize|keyboardHidden"
android:hardwareAccelerated="true"
android:icon="#drawable/ecn_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MapActivity"
android:icon="#drawable/ecn_icon"
android:label="test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings">
</activity>
<service
android:name=".UnZipService"
android:exported="false"/>
</application>
In activity, I have
IntentFilter intentFilter = new IntentFilter(DownloadManager
.ACTION_DOWNLOAD_COMPLETE);
receiverDownloadComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager
.EXTRA_DOWNLOAD_ID, -1);
if (myDownloadReference == reference) {
...
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
break;
...
}
cursor.close();
}
}
};
registerReceiver(receiverDownloadComplete, intentFilter);
And service here:
public class UnZipService extends IntentService {
public UnZipService() {
super("UnZipService");
}
#Override
protected void onHandleIntent(Intent workIntent) {
String dataString = workIntent.getDataString();
Log.v("IntentURI", dataString);
Toast.makeText(this, "Installing....", Toast.LENGTH_SHORT).show();
}
It should show toast just for test, but I always get an error:
01-29 19:08:25.740 15521-15521/org.osmdroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=org.osmdroid (has extras) } in org.osmdroid.SettingsActivity$1#21292650
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5147)
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: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.osmdroid/org.osmdroid.UnZipService}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
at android.app.Activity.startActivityForResult(Activity.java:3404)
at android.app.Activity.startActivityForResult(Activity.java:3365)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:3600)
at android.app.Activity.startActivity(Activity.java:3568)
at org.osmdroid.SettingsActivity$1.onReceive(SettingsActivity.java:148)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:150)
            at android.app.ActivityThread.main(ActivityThread.java:5147)
            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)
Both (class and activity) in same folder (org.osmdroid). Seems like paths is ok, but the problem appears and I have no more ideas...
You are starting a service as an activity
Change
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startActivity(mServiceIntent);
to
Intent mServiceIntent = new Intent(context, UnZipService.class);
mServiceIntent.setData(Uri.parse(savedFilePath));
startService(mServiceIntent); // Only this line is changed
Please declared this activity in your AndroidManifest.xml

Unfortunately, App has stopped (Android Development)

I just started android development in 2 days and this is the error that I cannot solve "Unfortunately, [App] has stopped".
This is what I'm planning to do with the APP.
show the UI that will tell the user that by pressing ok the app will start making a log file.
when the OK button is pressed, it will run in the background (don't need UI) and will wait for the notification from other app and will write the text from the notification to the log file.
This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationnotifier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.notificationnotifier.GetNotification"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.notificationnotifier.MonitorNotification"
android:label="#string/app_name" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
This is GetNotification.java
public class GetNotification extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_notification);
Button okButton = (Button) findViewById(R.id.OKButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
startActivity(getNotification);
}
}) ;
Button cancel = (Button) findViewById(R.id.Cancel);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}) ;
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
This is MonitorNotification.java
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;
public class MonitorNotification extends AccessibilityService{
#Override
public void onAccessibilityEvent(AccessibilityEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
#Override
protected void onServiceConnected() {
// TODO Auto-generated method stub
// super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.feedbackType = 1;
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.notificationTimeout = 100;
setServiceInfo(info);
}
}
This is the logcat
[updated]
02-04 10:31:17.947: E/Trace(1108): error opening trace file: No such file or directory (2)
02-04 10:31:18.648: D/gralloc_goldfish(1108): Emulator without GPU emulation detected.
02-04 10:31:21.128: D/AndroidRuntime(1108): Shutting down VM
02-04 10:31:21.148: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-04 10:31:21.169: E/AndroidRuntime(1108): FATAL EXCEPTION: main
02-04 10:31:21.169: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.notificationnotifier/com.example.notificationnotifier.MonitorNotification}: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Looper.loop(Looper.java:137)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 10:31:21.169: E/AndroidRuntime(1108): at dalvik.system.NativeStart.main(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): Caused by: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-04 10:31:21.169: E/AndroidRuntime(1108): ... 11 more
02-04 10:31:23.268: I/Process(1108): Sending signal. PID: 1108 SIG: 9
Replace this line:
Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
by this one:
Intent getNotification = new Intent(GetNotification.this,
MonitorNotification.class);
Hope it helps.
Add the below code in your manifest file:
<service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
. . .
</service>
Declaration
An accessibility is declared as any other service in an AndroidManifest.xml but it must also specify that it handles the "android.accessibilityservice.AccessibilityService" Intent. Failure to declare this intent will cause the system to ignore the accessibility service. Additionally an accessibility service must request the BIND_ACCESSIBILITY_SERVICE permission to ensure that only the system can bind to it. Failure to declare this intent will cause the system to ignore the accessibility service. Following is an example declaration:
<service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
. . .
</service>
I know now what the problem was, my friend told me that it should be startService not startActivity in the getNotification.java
the correct way to call it is
startService(getNotification);
not
startActivity(getNotification);
I'm extending AccesibilityService in the class in MonitorNotification.java. :D

Android App crashing in Emulator [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm a novice android app developer. I wrote an application which fetches Bible Readings from online. I wrote the separate Java class for fetching the reading and I tested it and it's working fine. But when I tried to use that in my Android App, my app crashed and stops working. I've understood from the logs that it's throwing a null pointer exception, but I don't how to fix that. I've also checked the internet and I can't find an exact solution for this. I'm attaching the logs and file related to my application below. Could anyone go through my code and throw some light on fixing the error. I appreciate your time on my behalf and many thanks in advance.
10-25 09:49:23.243: E/AndroidRuntime(620): FATAL EXCEPTION: main
10-25 09:49:23.243: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mgocsm/com.mgocsm.EveningKymthaPrayers}: java.lang.NullPointerException: println needs a message
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-25 09:49:23.243: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 09:49:23.243: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 09:49:23.243: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-25 09:49:23.243: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-25 09:49:23.243: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
10-25 09:49:23.243: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException: println needs a message
10-25 09:49:23.243: E/AndroidRuntime(620): at android.util.Log.println_native(Native Method)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.util.Log.d(Log.java:138)
10-25 09:49:23.243: E/AndroidRuntime(620): at com.mgocsm.BibleReader.getVerses(BibleReader.java:44)
10-25 09:49:23.243: E/AndroidRuntime(620): at com.mgocsm.EveningKymthaPrayers.onCreate(EveningKymthaPrayers.java:25)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.Activity.performCreate(Activity.java:5008)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-25 09:49:23.243: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-25 09:49:23.243: E/AndroidRuntime(620): ... 11 more
EveningKymthaPrayers.java (Activitiy from where I call the Java class to fetch readings)
=========================
.
public class EveningKymthaPrayers extends Activity {
AssetManager am;
FileReader f;
TextView kymtha_prayer;
BibleReader bible;
//ReadingList rl = new ReadingList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.evening_kymtha);
am = getAssets();
f = new FileReader(am,"kymtha.txt");
kymtha_prayer = (TextView) findViewById(R.id.show_kymtha);
kymtha_prayer.setMovementMethod(new ScrollingMovementMethod());
bible = new BibleReader("John 3:16-17");
kymtha_prayer.setText(bible.getVerses());
//kymtha_prayer.setText(f.readFile());
//kymtha_prayer.setText(setText);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
BibleReader.java (Java class for fetching Bible verses)
public class BibleReader {
public static final String URL="http://labs.bible.org/api/?passage=";
String verse;
String logName = "BibleReader";
String line;
StringBuilder recText;
private static final int BUFFER_SIZE = 1024 * 10;
private final byte[] dataBuffer = new byte[BUFFER_SIZE];
public BibleReader(String verse) {
// TODO Auto-generated constructor stub
this.verse = verse;
this.verse = this.verse.toString().replaceAll(" ", "%20");
}
public String getVerses()
{
String body = null;
try{
URL url = new URL(URL+this.verse);
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while ((len = in.read(dataBuffer)) != -1) {
baos.write(dataBuffer, 0, len);
}
body = new String(baos.toByteArray(), encoding);
Log.d(logName, "Fetched Verse"+body);
}
catch(Exception e){
Log.d(logName, e.getMessage());
}
return body.toString();
}
}
AndroidMainfest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mgocsm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DailyPrayers" android:label="#string/app_name"></activity>
<activity android:name=".EveningPrayers" android:label="#string/app_name"></activity>
<activity android:name=".EveningKymthaPrayers" android:label="#string/app_name"></activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
switch(v.getId()){
case R.id.daily_prayers:
startActivity(new Intent(getApplicationContext(),DailyPrayers.class));
break;
}
}
}
DailyPrayers.java
public class DailyPrayers extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_prayers);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
switch(v.getId()){
case R.id.home:
startActivity(new Intent(getApplicationContext(),MainActivity.class));
break;
case R.id.evening_prayers:
startActivity(new Intent(getApplicationContext(),EveningPrayers.class));
break;
}
}
}
EveningPrayers.java
public class EveningPrayers extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.evening_prayers);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
switch(v.getId()){
case R.id.kymtha_evening:
startActivity(new Intent(getApplicationContext(),EveningKymthaPrayers.class));
break;
case R.id.sleeba_evening:
break;
}
}
}
The body that you are trying to print is getting null as a value.Check first you receive any value in body or not.
What i have noticed in your code:
bible.getVerses() is returning null
It seems INTERNET permission is not included in AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Don't make a web call on Main thread. Instead you can use AsyncTask or AsyncTaskLoader
Instead of using:
this.verse = this.verse.toString().replaceAll(" ", "%20");
You must use:
String webURL = URLEncoder.encode("your web url", "utf-8");
What are you logging at this place: com.mgocsm.BibleReader.getVerses(BibleReader.java:44)
?
You are probably passing a Null.
Add this permission in your Manifest
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
body = new String(baos.toByteArray(), encoding);
Log.d(logName, "Fetched Verse"+body);
your Body is getting null value that is why ur log is giving nullpointer exception

Android Java: Global variables through subclass; can't launch app?

I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others. I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.
I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse. All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity. Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.
Quick overview:
GlobalVars class extends Application
In the manifest, android:name has been added to reference the additional GlobalVars class
Within my main activity, I'm initializing the global vars class with getApplicationContext()
Here is everything I've got; any help would be much appreciated!
MainActivity.java
package com.example.generaltest1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
GlobalVars.java
package com.example.generaltest1;
import android.app.Application;
public class GlobalVars extends Application {
String myString = "Some Text";
public String getMyString() {
return myString;
}
public String setMyString(String string) {
this.myString = string;
return myString;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">GeneralTest1</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
GeneralTest1 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.generaltest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:name="GlobalVars">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Log Cat
08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.example.generaltest1.MainActivity.<init>(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620): ... 11 more
You are calling getApplicationContext() in your class definition, which is too early in the activity lifetime and results in a nullpointer exception. Move the assignments to your onCreate() function and it should work as expected.
Please check this article about activities and their lifecycle :
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Your activity should look like this:
public class MainActivity extends Activity {
GlobalVars myVars;
TextView myText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVars = ((GlobalVars)getApplication());
myText = (TextView) findViewById(R.id.myText);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Add these lines in oncreate()..
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);

Categories