I have an app that worked up to just recently.
The error in the Google play store is as follows:
java.lang.RuntimeException: Unable to instantiate application org.marsfirst.mars_app: java.lang.ClassNotFoundException: Didn't find class "org.marsfirst.mars_app" on path: DexPathList[[zip file "/data/app/org.marsfirst.mars_app-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.marsfirst.mars_app-2, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4703)
at android.app.ActivityThread.access$1600(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.marsfirst.mars_app" on path: DexPathList[[zip file "/data/app/org.marsfirst.mars_app-2.apk"],nativeLibraryDirectories=[/data/app-lib/org.marsfirst.mars_app-2, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newApplication(Instrumentation.java:981)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
MainActivity:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;//this gives me a warning that states its unused but it has been used in the past
import android.support.v4.app.Fragment;//this gives me a warning that states its unused but it has been used in the past
import android.app.Activity;//this gives me a warning that states its unused but it has been used in the past
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.LogInCallback;
public class MainActivity extends ActionBarActivity {
private String un, pw;
public TextView username;
public EditText pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.editloginusername);
pass = (EditText) findViewById(R.id.editpass);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
un = username.getText().toString();
pw = pass.getText().toString();
ParseUser.logInInBackground(un, pw, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
new CallMainPage().execute();
} else {
// username.setText("");
pass.setText("");
makeToast("User name or password not recognized.");
}
}
});
}
});
Button signup = (Button) findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new CallSignup().execute();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class CallSignup extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... urls) {
Intent intent = new Intent(MainActivity.this, Signup.class);
MainActivity.this.startActivity(intent);
return "";
}
}
public class CallMainPage extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... urls) {
Intent intent = new Intent(MainActivity.this, MainPage.class);
MainActivity.this.startActivity(intent);
return "";
}
}
public void makeToast(String text) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
My manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="org.marsfirst.mars_app"
android:protectionLevel="signature" />
<uses-permission android:name="org.marsfirst.mars_app" />
<application
android:name="org.marsfirst.mars_app"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="org.marsfirst.mars_app.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=".Signup"
android:label="#string/signup"
android:parentActivityName=".MainActivity" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".MainPage"
android:label="Main Page" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".Webpage"
android:label="Website"
android:parentActivityName=".MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainPage" />
</activity>
<activity
android:name=".TeamNews"
android:label="Team News"
android:parentActivityName="org.marsfirst.mars_app.MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="org.marsfirst.mars_app.MainPage" />
</activity>
<activity
android:name=".OprPage"
android:label="OPR Page"
android:parentActivityName="org.marsfirst.mars_app.MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="org.marsfirst.mars_app.MainPage" />
</activity>
<activity
android:name="org.marsfirst.mars_app.MechPage"
android:label="Mechanical Page"
android:parentActivityName="org.marsfirst.mars_app.MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainPage" />
</activity>
<activity
android:name=".DriveNews"
android:label="Drive News"
android:parentActivityName=".MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainPage" />
</activity>
<activity
android:name=".ProgramingPage"
android:label="Programming Page"
android:parentActivityName=".MainPage" >
<intent-filter />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainPage" />
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="org.marsfirst.mars_app" />
</intent-filter>
</receiver>
</application>
</manifest>
File Explorer
http://imgur.com/UCwiJqP
I am unsure if it is a Code error or not, due to my research I believe it is a buildpath error however everything I have tried seems to fail please help
I will post any Additional screen shots or code that is needed. In attempt to fix the problem one "fix" I found was to change every thing to android:name=".DriveNews" instead of android:name=org.marsfirst.mars_app.DriveNews which was what it was originally
Thank you for any help you may be able to give.
Disclaimer: I am a noob in android development.
I think you would like to verify the following permission tags:
<permission
android:name="org.marsfirst.mars_app"
android:protectionLevel="signature" />
<uses-permission android:name="org.marsfirst.mars_app" />
The classloader is not able to find a class org.marsfirst.mars_app. From your code it seems like mars_app is a package. Same for android:name in application name.
Change
android:name="org.marsfirst.mars_app"
to
android:name="org.marsfirst.mars_app.YourApplicationClass"
where YourApplicationClass is the class that is extending Application class. If you don't have any such class just remove the name attribute.
According to official documentation of the application tag the name attribute should be
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
can show you file explore?
e.g.
Right click the project -> Build Path -> Configure Build Path -> Order and Export and check the libraries that you use in your project.
Do you use proguard in your application?
if yes: check this from developer.google site. It may help you
The default proguard.cfg file tries to cover general cases, but you might encounter exceptions such as ClassNotFoundException, which happens when ProGuard strips away an entire class that your application calls.
You can fix errors when ProGuard strips away your code by adding a -keep line in the proguard.cfg file. For example:
-keep public class < MyClass >
Related
I've created a custom deep link dispatcher in Java within a react native project.
The issue I'm having is that I can't get MainActivity to start from my dispatcher, and I can't quite figure out why.
I have AndroidManifest set up so that the deep link is captured in LinkDispatcherActivity effectively, but the startActivity(dispatchedIntent) never reaches MainActivity, which is where I assume it needs to go. Ordinarily in AndroidManifest.xml, deep links would be sent to MainActivity).
It does open the app, but I assume it's because LinkDispatcherActivity is part of the app? Not sure, if I say something dumb it's because this is only my 3rd day in a row writing Java 😰
Below is my code.
package com.example.app;
//Inspired by https://github.com/justeat/Android.Samples.Deeplinks, Licensed under Apache 2.0
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import net.openid.appauth.RedirectUriReceiverActivity;
public class LinkDispatcherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
dispatchIntent(getIntent());
} catch (IllegalArgumentException iae) {
// Malformed URL
if (BuildConfig.DEBUG) {
Log.e("Deep links", "Invalid URI", iae);
}
} finally {
// Always finish the activity so that it doesn't stay in our history
finish();
}
}
public void dispatchIntent(Intent intent) {
final Uri uri = intent.getData();
final String host = uri.getHost().toLowerCase();
if (uri == null) throw new IllegalArgumentException("Uri cannot be null");
// Default intent
Intent dispatchedIntent = new Intent(LinkDispatcherActivity.this, MainActivity.class);
dispatchedIntent.setData(uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d(dispatchedIntent.getDataString(), "dispatchIntent: intent string");
// Auth intent only, this is why I need the dispatcher
if ("login".equals(host)) {
dispatchedIntent = new Intent(this, RedirectUriReceiverActivity.class);
dispatchedIntent.putExtra("requestCode", 0);
Log.d("Login", "mapAppLink: ");
startActivityForResult(dispatchedIntent, 0);
return;
}
Log.d("Default", "mapAppLink: ");
startActivity(dispatchedIntent);
}
}
This is what my AndroidManifest.xml looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.example.app">
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MainApplication"
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/AppTheme"
android:requestLegacyExternalStorage="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="<redacted>" />
<!-- < Notification Services > -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#mipmap/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
tools:replace="android:resource"
android:resource="#color/colorAccent" />
<!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="true"/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
tools:replace="android:value"
android:value="rn-push-notification-channel-id" />
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name" android:value="Default Channel"/>
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description" android:value="Default channel for push notifications"/>
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="#color/colorAccent" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<!-- START: Add this-->
<service
android:name=".MainNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:name="com.intercom.reactnative.RNIntercomPushBroadcastReceiver"
tools:replace="android:exported"
android:exported="true"/>
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|screenSize|uiMode"
android:launchMode="singleTask"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</activity>
<activity android:name=".LinkDispatcherActivity"
tools:node="replace">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="example"
android:host="login" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
Edit: for the intent, I've tried:
new Intent(getApplicationContext(), MainActivity.class)
new Intent(reactContext, MainActivity.class) (extending ReactActivity instead of Activity)
new Intent(this, MainActivity.class)
new Intent("MainActivity")
Try adding RedirectUriReceiverActivity to the manifest - I couldn't see it in there.
<activity android:name="com.facebook.react.devsupport.RedirectUriReceiverActivity" />
This is what I ended up with for the LinkDispatcherActivity. To summarize, what I did was dispatch the intents that the deep activities were eventually supposed to invoke directly, instead of trying to forward the intent. In other words, if you're trying to pass an intent from one activity to another, maybe don't and consider dispatching the activity/function/intent that you want to run afterwards. I still think forwarding the intent should have worked, but this is what ended up working.
In order to accomplish the above, I:
Called a function within the AppAuth package that generates an intent, and dispatch it
Used ReactContext to emit the url listener event that React Native uses to respond to deep links.
Below is the code:
//Inspired by https://github.com/justeat/Android.Samples.Deeplinks, Licensed under Apache 2.0
package com.example.app;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import net.openid.appauth.AuthorizationManagementActivity;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class LinkDispatcherActivity extends ReactActivity {
#Override
protected void onResume() {
super.onResume();
try {
dispatchIntent(getIntent());
} catch (IllegalArgumentException iae) {
// Malformed URL
if (BuildConfig.DEBUG) {
Log.e("Deep links", "Invalid URI", iae);
}
} finally {
// Always finish the activity so that it doesn't stay in our history
finish();
}
}
public void dispatchIntent(Intent intent) {
final Uri uri = intent.getData();
final String host = uri.getHost().toLowerCase();
if (uri == null) throw new IllegalArgumentException("Uri cannot be null");
// Default intent, e.g. settings, fitbit
// wait for react context
ReactInstanceManager reactInstanceManager = getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
Intent dispatchedIntent = new Intent(getApplicationContext(), MainActivity.class);
dispatchedIntent.setData(uri);
dispatchedIntent.putExtra("url", uri.toString());
dispatchedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Auth intent only
if ("login".equals(host)) {
Intent authIntent = AuthorizationManagementActivity.createResponseHandlingIntent(getApplicationContext(), uri);
startActivity(authIntent);
return;
}
// Below from https://stackoverflow.com/questions/48445010/send-data-from-android-activity-to-react-native?rq=1
WritableMap uriMap = Arguments.createMap();
uriMap.putString("url", uri.toString());
if(reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("url", uriMap);
// If there's no context, create a listener to wait for it
} else {
reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
#Override
public void onReactContextInitialized(ReactContext context) {
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("url", uriMap);
reactInstanceManager.removeReactInstanceEventListener(this);
}
});
}
}
}
I built an app and I have different classes, and first I got an error like this when I tried to run the app
Cannot fit requested classes in a single dex file (# methods: 66087 > 65536)
I followed some instructions from Google and I added to my build.gradle
this dependency
implementation 'com.android.support:multidex:1.0.3'
and to the defaultConfig this
multiDexEnabled true
In my manifest xml I added this
android:name="android.support.multidex.MultiDexApplication"
but its highlighted with red color and when I move the cursor over it says
Unresolved class MultiDexApplication
When I try to run the app again it throws me
No file known for: classes2.dex
Known maps: {RelativeFile{base=/Users/***/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeDexDebug/out/classes.dex, path=classes.dex, type=DIRECTORY}=classes.dex, RelativeFile{base=/Users/****/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeProjectDexDebug/out/classes.dex, path=classes.dex, type=DIRECTORY}=classes3.dex, RelativeFile{base=/Users/*****/AndroidStudioProjects/****/app/build/intermediates/dex/debug/mergeExtDexDebug/out/classes2.dex, path=classes2.dex, type=DIRECTORY}=classes4.dex}
This is the MainActivity Class
package com.example.drivieapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, screenNotLogged.class );
Thread timer = new Thread() {
public void run() {
try {
sleep(3100);
}
catch(InterruptedException e) {
e.printStackTrace();
}
finally {
startActivity(i);
finish();
}
}
};
timer.start();
}
}
Can you please explain me what I have to do? I google the solution but didn't understand too much. Thank you very much!
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="****">
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Drivie"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>
<activity android:name=".SignupCarrier"></activity>
<activity android:name=".carrierSignupHint" />
<activity android:name=".home" />
<activity android:name=".SignupSender" />
<activity android:name=".signup" />
<activity android:name=".screenNotLogged" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have tried this on a few android phones with no luck (i did start the app once).
This was mostly generated from android studio 1.3.2 and from some questions like this and that and the other.
Perhaps someone can point out what i am doing wrong.
Thanks
edit: moved receiver inside app, but no joy.
edit2: added missing permissions for receiver. now seems to work on a nexus 4. but not on the kindle fire or the at&t tablets. although it seems to come right away if i press the circle icon on the azpen.
edit3: seems to work sometimes on the fires now.
package acme.startup;
import android.content.*;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
package acme.startup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
</application>
</manifest>
Every Component must be declared inside application tag into manifest. Here you declared receiver outside of application tag. Read Structure of the Manifest File.
Correct manifest wil be
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have to add permission to manifest file. Add this before your <application> tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Strangely map=frag.getMap(); does not throw nullpointerexception , but still the map is always null!
The same situation with or without running the handler.
I see the map (world map) but cannot do anything with the map object.
PRINT SCREE with project properties : http://i.stack.imgur.com/BGB65.jpg
Please have a look at the code and please advice where I am wrong.
package com.bestsiteeditor.bestwirelessdeals;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
public class Settings extends FragmentActivity{
LinearLayout submenu;
GoogleMap map;
LinearLayout mainLayout;
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
submenu =(LinearLayout) findViewById(R.id.submenu);
SubMenu sm = new SubMenu(this,submenu);
sm.getButtons();
mainLayout =(LinearLayout) findViewById(R.id.temp);
final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run() {
MapFragment frag = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(mainLayout.getId(), frag);
fragmentTransaction.commit();
map=frag.getMap();
System.out.println("THEMAAP "+map);
if(map != null) {
System.out.println("THEMAAP NOT NULL "+map);
}else{handler.postDelayed(this, 500);}
}
}, 500);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
return ActionBar.HandleMenu(this, item.getItemId());
}
}
and the is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bestsiteeditor.bestwirelessdeals"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<permission
android:name="com.suntrainer.suntrainer.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.suntrainer.suntrainer.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyAppActionBarTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDJuwvAd8Zzqasdasdadasdasd" />
<activity
android:name="com.bestsiteeditor.bestwirelessdeals.MainActivity"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.bestsiteeditor.bestwirelessdeals.Locations"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait" >
<intent-filter>
<action android:name="android.intent.action.LOCATIONS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.bestsiteeditor.bestwirelessdeals.Settings"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait" >
<intent-filter>
<action android:name="android.intent.action.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.bestsiteeditor.bestwirelessdeals.WatchList"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait" >
<intent-filter>
<action android:name="android.intent.action.WATCHLIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.bestsiteeditor.bestwirelessdeals.Profile"
android:label="#string/app_name"
android:screenOrientation="sensorPortrait" >
<intent-filter>
<action android:name="android.intent.action.PROFILE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You have
android:minSdkVersion="12"
So you need to change
public class Settings extends FragmentActivity{
to
public class Settings extends Activity{
Also it is better you check the availability of google play services before initializing
google map object.
From the docs
public final GoogleMap getMap ()
Gets the underlying GoogleMap that is tied to the view wrapped by this
fragment.
Returns the GoogleMap. Null if the view of the fragment is not yet
ready. This can happen if the fragment lifecyle have not gone through
onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also
happen if Google Play services is not available. If Google Play
services becomes available afterwards and the fragment have gone
through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this
method again will initialize and return the GoogleMap.
Edit:
Missing a meta tag in the applciation tag of manifest fie.
Pls make sure you followed all the steps #
https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Simply waiting 500 ms after the fragment transaction solved the issue.
Instead of Thread ,handler could be also implemented also.
fragmentTransaction.commit();
Thread thread= new Thread(){
#Override
public void run(){ try { synchronized(this){wait(500);}}catch(InterruptedException ex){ }
map=frag.getMap();
System.out.println("MAP OK"+frag.getMap());
}
};
thread.start();
Hi im trying to make it that when a user clicks on on a button the phone vibrates. Heres manifest.xml and AndroidVibrator.java so whats wrong? how can i fix it? thanks!
heres the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".AndroidActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity2"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".activity3"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".next" >
</activity>
</application>
</manifest>
Heres AndroidActivity:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.os.Vibrator;
public class AndroidVibrator extends Activity implements OnClickListener
{
private View myView;
private Vibrator myVib;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
//myView can be any type of view, button, etc.
myView = (View) this.findViewById(R.id.sound);
myView.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View v)
{
myVib.vibrate(50);
//add whatever you want after this
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
Make sure you call
<uses-permission android:name="android.permission.VIBRATE" />
In your manifest
EDIT:
Try putting your permission at the end of the manifest, or after version code.
I've responded to your previous post about the same matter
this time you place the permissions over uses sdk...
check my other answer.
Also check the ouput of LogCat. Does your application force closes?
may be 50 ms is too short to feel vibrate
try more than 50ms