I made a class and its constructor should take 2 arguments, but Eclipse gives me an InstantiationException and I don't know why. How can I fix this?
Here is the Sms class code:
public class Sms {
String message;
String phonenumber = "";
SharedPreferences sp;
Context context;
public Sms(String m, Context context)
{
message = m;
this.context = context;
}
public void sendSms()
{
sp = PreferenceManager.getDefaultSharedPreferences(context);
phonenumber = sp.getString("PHONE", "");
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH)
{
ArrayList<String> messagelist = manager.divideMessage(message);
manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
}
else
{
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
}
}
}
--------------------------------------------
And the LogCat:
02-16 17:20:26.579: E/AndroidRuntime(474): FATAL EXCEPTION: main
02-16 17:20:26.579: E/AndroidRuntime(474): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mioc.programing.safetpin/mioc.programing.safetpin.Sms}: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.os.Looper.loop(Looper.java:123)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.reflect.Method.invoke(Method.java:507)
02-16 17:20:26.579: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-16 17:20:26.579: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-16 17:20:26.579: E/AndroidRuntime(474): at dalvik.system.NativeStart.main(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): Caused by: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.Class.newInstanceImpl(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.Class.newInstance(Class.java:1409)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-16 17:20:26.579: E/AndroidRuntime(474): ... 11 more
And this is how I create the object from my Activity:
Intent open_SM = new Intent ("android.intent.action.SMS");
startActivity(open_SM);
Sms s = new Sms("trlababalan",this);
s.sendSms()
;
here is manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mioc.programing.safetpin"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="mioc.programing.safetpin.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>
<service android:name="TestService">
</service>
<activity
android:name="mioc.programing.safetpin.Sms"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You have declared Sms as an Activity in your Manifest:
<activity
android:name="mioc.programing.safetpin.Sms"
android:label="#string/app_name" >
But Sms doesn't extend Activity... So these lines cause the exception:
Intent open_SM = new Intent ("android.intent.action.SMS");
startActivity(open_SM);
If you want to run Sms as an Activity you need to turn the entire class into an Android Activity, or if you want Sms to be a utility don't use an Intent to start Sms as an Activity.
Related
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())
I have a custom View class in my application. I want to extend it and add it to my xml file however when I do that I get class not found exception..
Here is the xml file:
floorplan.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/floorplannextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="47dp"
android:layout_marginTop="22dp"
android:text="Next" />
<com.example.helloworld3.FloorPlan.DrawView2
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
And this is my DrawView2 class. It is inside another class called FloorPlan but the entire code would be too long to post
DrawView2.java
public class DrawView2 extends RelativeLayout {
Paint paint = new Paint();
public DrawView2(Context context) {
super(context);
paint.setColor(Color.RED);
}
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
for(int i =0,j=i+1 ; i<subpath.size()&& j<subpath.size(); i++, j++){
ux = subpath.get(i).v.x;
dx =subpath.get(i).v.y;
rx= subpath.get(j).v.x;
lx = subpath.get(j).v.y;
canvas.drawLine(ux, dx , rx, lx, paint);
}
}
This is what I get in the LogCat:
04-04 20:37:59.959: D/(884): HostConnection::get() New Host Connection established 0x8b92258, tid 884
04-04 20:38:07.047: W/KeyCharacterMap(884): No keyboard for id 0
04-04 20:38:07.047: W/KeyCharacterMap(884): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-04 20:38:16.599: D/AndroidRuntime(884): Shutting down VM
04-04 20:38:16.599: W/dalvikvm(884): threadid=1: thread exiting with uncaught exception (group=0xb5ff94f0)
04-04 20:38:16.607: E/AndroidRuntime(884): FATAL EXCEPTION: main
04-04 20:38:16.607: E/AndroidRuntime(884): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld3/com.example.helloworld3.FloorPlan}: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.helloworld3.FloorPlan.DrawView2
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.os.Looper.loop(Looper.java:130)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 20:38:16.607: E/AndroidRuntime(884): at dalvik.system.NativeStart.main(Native Method)
04-04 20:38:16.607: E/AndroidRuntime(884): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.helloworld3.FloorPlan.DrawView2
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.Activity.setContentView(Activity.java:1657)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.example.helloworld3.FloorPlan.onCreate(FloorPlan.java:164)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-04 20:38:16.607: E/AndroidRuntime(884): ... 11 more
04-04 20:38:16.607: E/AndroidRuntime(884): Caused by: java.lang.ClassNotFoundException: com.example.helloworld3.FloorPlan.DrawView2 in loader dalvik.system.PathClassLoader[/data/app/com.example.helloworld3-1.apk]
04-04 20:38:16.607: E/AndroidRuntime(884): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
04-04 20:38:16.607: E/AndroidRuntime(884): ... 20 more
And is my Manifest file:
AndroidManifes. xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".FloorPlan"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FloorPlanTwo"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLANTWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FloorPlanThree"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLANTHREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you for all you help!
If DrawView2 is an inner class it should be referred to in XML as:
...
<com.example.helloworld3.FloorPlan$DrawView2
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
I had a problem similar to yours. Try these two things:
Extends from View, not from RelativeLayout
Add these constructors to your DrawView2 class
public DrawView2(Context context)
{
super(context);
}
public DrawView2(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public DrawView2(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
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
Iḿ having troubles to fix this, the message arrive to the device, but when it appear the notification, the app crash with this error ->
Error receiving broadcast Intent { act=PATH.DISPLAY_MESSAGE flg=0x10 (has extras)
I have posted some parts of the code, if it's neccesary I can post more
Thanks to all
David
NOTIFICATION GENERATE
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
On Message
#Override
protected void onMessage(Context context, Intent intent) {
if(aController == null)
aController = (Controller) getApplicationContext();
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
aController.displayMessageOnScreen(context, message);
// notifies user
generateNotification(context, message);
}
CONTROLLER VOID display Message
// Notifies UI to display a message.
void displayMessageOnScreen(Context context, String message) {
Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION);
intent.putExtra(Config.EXTRA_MESSAGE, message);
// Send Broadcast to Broadcast receiver with message
context.sendBroadcast(intent);
}
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PATH"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:name="PATH.Controller"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="PATH.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PATH" />
</intent-filter>
</receiver>
<service android:name="PATH.GCMIntentService" />
<activity
android:name="PATH.SplashScreen"
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.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="PATH.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="PATH.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
</manifest>
01-09 22:03:21.315: E/AndroidRuntime(13801): java.lang.RuntimeException: Error receiving broadcast Intent { act=PATH.DISPLAY_MESSAGE flg=0x10 (has extras) } in PATH.MainActivity$1#41a10b28
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Handler.handleCallback(Handler.java:605)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Looper.loop(Looper.java:137)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.app.ActivityThread.main(ActivityThread.java:4517)
01-09 22:03:21.315: E/AndroidRuntime(13801): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 22:03:21.315: E/AndroidRuntime(13801): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 22:03:21.315: E/AndroidRuntime(13801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-09 22:03:21.315: E/AndroidRuntime(13801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-09 22:03:21.315: E/AndroidRuntime(13801): at dalvik.system.NativeStart.main(Native Method)
01-09 22:03:21.315: E/AndroidRuntime(13801): Caused by: java.lang.NullPointerException
01-09 22:03:21.315: E/AndroidRuntime(13801): at PATH.MainActivity$1.onReceive(MainActivity.java:140)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
It works comenting this controller lines.
#Override
protected void onMessage(Context context, Intent intent) {
/*if(aController == null)
aController = (Controller) getApplicationContext();*/
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
// aController.displayMessageOnScreen(context, message);
// notifies user
generateNotification(context, message);
}
I searched a lot for similar kind of error, but I am not getting a solution for it. I am getting this error as soon as I start the app
03-21 10:33:08.100: E/AndroidRuntime(13098): FATAL EXCEPTION: main
03-21 10:33:08.100: E/AndroidRuntime(13098): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.airlife/com.example.airlife.MainActivity}: java.lang.ClassNotFoundException: com.example.airlife.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.airlife-2.apk]
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.os.Looper.loop(Looper.java:150)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread.main(ActivityThread.java:4389)
03-21 10:33:08.100: E/AndroidRuntime(13098): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:33:08.100: E/AndroidRuntime(13098): at java.lang.reflect.Method.invoke(Method.java:507)
03-21 10:33:08.100: E/AndroidRuntime(13098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-21 10:33:08.100: E/AndroidRuntime(13098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-21 10:33:08.100: E/AndroidRuntime(13098): at dalvik.system.NativeStart.main(Native Method)
03-21 10:33:08.100: E/AndroidRuntime(13098): Caused by: java.lang.ClassNotFoundException: com.example.airlife.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.airlife-2.apk]
03-21 10:33:08.100: E/AndroidRuntime(13098): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-21 10:33:08.100: E/AndroidRuntime(13098): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-21 10:33:08.100: E/AndroidRuntime(13098): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
03-21 10:33:08.100: E/AndroidRuntime(13098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
03-21 10:33:08.100: E/AndroidRuntime(13098): ... 11 more
MainActivity.java is like this
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends MapActivity implements OnClickListener{
private ImageButton mVid,mAgebtn,mWeightbtn,mPicbtn,mMsgbtn,mReqbtn,mHelibtn,mDocbtn,mContextbtn;
private MapView mapView;
private MyLocationOverlay myLocationOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView)findViewById(R.id.webview);
mapView.setBuiltInZoomControls(true);
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
// call convenience method that zooms map on our location
zoomToMyLocation();
mAgebtn = (ImageButton) findViewById(R.id.imageView36);
mWeightbtn = (ImageButton) findViewById(R.id.imageView4);
mPicbtn = (ImageButton) findViewById(R.id.imageView5);
mMsgbtn = (ImageButton) findViewById(R.id.imageView6);
mReqbtn = (ImageButton) findViewById(R.id.imageView32);
mHelibtn = (ImageButton) findViewById(R.id.btnsearch);
mDocbtn = (ImageButton) findViewById(R.id.btnprovide);
mVid = (ImageButton) findViewById(R.id.btnpost);
mContextbtn = (ImageButton) findViewById(R.id.btnsettings);
mAgebtn.setOnClickListener(this);
mWeightbtn.setOnClickListener(this);
mPicbtn.setOnClickListener(this);
mMsgbtn.setOnClickListener(this);
mReqbtn.setOnClickListener(this);
mHelibtn.setOnClickListener(this);
mDocbtn.setOnClickListener(this);
mVid.setOnClickListener(this);
mContextbtn.setOnClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
// when our activity resumes, we want to register for location updates
myLocationOverlay.enableMyLocation();
}
#Override
protected void onPause() {
super.onPause();
// when our activity pauses, we want to remove listening for location updates
myLocationOverlay.disableMyLocation();
}
/**
* This method zooms to the user's location with a zoom level of 10.
*/
private void zoomToMyLocation() {
GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
if(myLocationGeoPoint != null) {
mapView.getController().animateTo(myLocationGeoPoint);
mapView.getController().setZoom(10);
}
else {
Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.airlife"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.airlife.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=".playvid" />
<activity android:name=".Display" />
<activity android:name=".Video" />
<activity android:name=".Documents" />
<activity android:name=".Contact" />
<activity android:name=".FixedMyLocationOverlay" />
</application>
</manifest>
Please tell me why I am getting this error, and how to solve this.
Got the solution. I had missed this code in my manifest file
<uses-library android:name="com.google.android.maps"/>
Class not found exception in MainActivity.
I think you've the wrong package, or that your APK doesn't have what you think it has.This is your package name.
com.example.airlife
Maku sure that it is imported in all activities as.
package com.example.airlife;
and don't forget to add entry in manifest file for every new activity.
If so, then make this change in manifest file. No need to mention your package name and remove allowBackUp
<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>
if not this try adding any library files you are using. missing library files may also cause this exception. see this link
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo after SDK update