here I add parse library to my application. but in my main activity i faced an error with parse.initialize .
public void onCreate() {
Parse.initialize(this, "id",
"id");
PushService.setDefaultPushCallback(this, MyActivity.class);
}
You're using initialize in your activity, you should rather try in your Application class, from the docs
The recommended way is to put a call to Parse.initialize in your
Application's onCreate method.
An example:
import android.app.Application;
import com.parse.Parse;
public class MyApplication extends Application {
public void onCreate() {
Parse.initialize(this, "your application id", "your client key");
}
}
you should also add to your manifest
<application
android:name="YourPackageName.MyApplication"
</application>
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm trying to programatically call Home button with another button inside my project. App compiles fine, but when I tap the button that should call Home I receive following error:
Attempt to invoke virtual method 'void
android.content.Context.startActivity(android.content.Intent)' on a
null object reference
Here is my code (just essentials):
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
public class ClassName extends ReactContextBaseJavaModule {
private Context context;
#ReactMethod
public void minApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}
What am I doing wrong?
SOLUTION:
Due to the fact that my app uses react native, the code in bridged method in java file should look as below:
import android.content.Intent;
import android.content.Context;
public class ClassName extends ReactContextBaseJavaModule {
#ReactMethod
public void minApp() {
Context context = getReactApplicationContext();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}
Using this we can assign Home button function anywhere we want ;)
public void openLauncher(Context context) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
You can just open the launcher using this function.
Seems like you haven't initialized context. Make sure to initiate it before calling minApp()
in a separate class not being activity there is a method for authorization on Twitter
public void login(){
twitterAuthClient.authorize(new Activity() {
#Override
public void startActivityForResult(#RequiresPermission Intent intent, int requestCode) {
fragment.startActivityForResult(intent, requestCode);
}
#Override
public PackageManager getPackageManager() {
return fragment.getContext().getPackageManager();
}
}, twitterAuthCallback);
}
So here when this method works
<activity android:name="com.twitter.sdk.android.core.identity.OAuthActivity" />, an error occurs
Unable to find explicit activity class {/com.twitter.sdk.android.core.identity.OAuthActivity}; Have you declared this activity in your AndroidManifest.xml?
Even after adding an actives file to the manifest, the error does not disappear.
I ask your help.
You Have to declare the OAuthActivity in your project's manifest file. Please putting the following lines in Androidmanifest.xml should work.
<activity android:name="com.twitter.sdk.android.core.identity.OAuthActivity" />
So i've been stuck on this problem for literally days. I have integrated Unity with Eclipse IDE and i can build and deploy projects perfectly. However, im trying to start an a basic Intent on the java side and trigger it on the Unity/C# side.
Here's my code for the Java side:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerNativeActivity;
public class AppLauncher extends UnityPlayerNativeActivity
{
public Intent myIntent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Assuming that we want to launch the browser to browse a website
Uri uri = Uri.parse("http://www.google.com");
myIntent= new Intent(Intent.ACTION_VIEW, uri);
}
public void Trigger()
{
startActivity(myIntent);
}
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
And here's my code for the C# side of if it:
if(s[0].Equals("Spr"))
{
print("Launched");
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("Trigger");
}
Here's the error im getting thrown by logcat when the C# trigger is hit:
AndroidJavaException: java.lang.NoSuchMethodError: no method with name='Trigger' signature='()V' in class Lcom/unity3d/player/UnityPlayerNativeActivity;
I've tried screwing around by passong a custom signature along with the Trigger method name in the C# script, ive tried extending the standard UnityPlayerActivity, etc... I've tried hours worth of stuff and i Cannot seem to solve tis problem.
Any help is greatly accepted!
Are you sure that your activity is created? Do you really need to extend the activity? I'd try something like this:
Java:
package my.package;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class AppLauncher
{
public static void Trigger()
{
//Assuming that we want to launch the browser to browse a website
Uri uri = Uri.parse("http://www.google.com");
Intent myIntent= new Intent(Intent.ACTION_VIEW, uri);
startActivity(myIntent);
}
}
C#:
AndroidJavaObject jo = new AndroidJavaObject("my.package.AppLauncher");
jo.CallStatic("Trigger");
I know this has been asked a million times before, but nothing is working for me. I have a service in a separate class that needs to be started when a button is pushed, after an application is launched from a LaunchIntent.
Long story short, here's my goal:
run commands>wait three seconds for commands to run>launch app>start service
The service is to monitor for the CONFIGURATION_CHANGED broadcast.
Manifest (the parts that matter):
</activity>
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED" >
</action>
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
MyService.java:
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.app.Service;
public class MyService extends Service {
String[] commandsdefault = {"/x"};
public void onCreate() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
public void onDestroy() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
public void onReceive(Context context, Intent intent) {
MainActivity ogres = new MainActivity();
ogres.RunAsRoot(commandsdefault);
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
And then I simply have the following line in my MainActivity.java to call the service to start:
startService(new Intent(getApplicationContext(), MyService.class));
I am more confused than a mosquito in a mannequin shop. LogCat is returning absolutely nothing helpful other than u=0 not found.
Do I have something incorrect here? I'm not even seeing toasts from the service starting.
Make sure your service is declared in Android manifest
try to Override the onStartCommand() method of your service. hope this helps
Try specifying the full name of your service as the android:name attribute (e.g. android:name="com.example.MyService")
Ok, solved my own question.
MyService was running the whole time! I just didn't see the toast notifications to alert me when it started. Now I have a monitor in my main Activity that posts a toast when the service is started/killed, rather than using MyService itself to post toasts.
First off am a complete newbie to Android coding and the logic is really confusion me. Originally im Flash developer and im familiar with concepts from there, while Android is complete set of new concepts. For example (correct me if im wrong) a Intent is like an Event and BroadcastReceiver is a EventListener ?
Well it is here im stuck, if it is so, Intent is Event and broadcastReceiver is eventListener then my question is how do i assign a variable, data that ive handeled in onReceive method ?
Ive been searching for long time now and really get angry at myself for not understanding the logic. Im trying to compare and associate things to ActionScript3 and Javascript (some stuff in JS is pretty close to AS3).
Now to the code im trying to write and the problem i got.
Im trying to get myself into writing a Android Native Extension for Adobe AIR...
So, far so good at least in some way :)
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.as3breeze.air"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".BluetoothExtension"
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>
As i see, the main activity is my BluetoothExtension.java which is following:
Notice it implements FREExtension (created by Adobe for Native Extensions)
package com.as3breeze.air;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;
import com.as3breeze.air.BluetoothExtensionContext;
public class BluetoothExtension implements FREExtension {
protected BluetoothExtensionContext BEC;
/** Called when the activity is first created. */
#Override
public FREContext createContext(String arg0) {
BEC = new BluetoothExtensionContext();
return BEC;
}
#Override
public void dispose() {
// TODO Auto-generated method stub
BEC.dispose();
BEC = null;
}
#Override
public void initialize() {}
}
Thats the Activity, right ?
And it creates Context which is following (am leaving out the #imports):
public class BluetoothExtensionContext extends FREContext {
public BluetoothAdapter bluetooth;
public Activity extensionActivity;
public FREArray nonBoundedDevices;
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public Map<String, FREFunction> getFunctions() {
Map<String, FREFunction> functionMap=new HashMap<String, FREFunction>();
functionMap.put("initialize", new Initialize());
// Leaving out some stuff here and listing only the important things...
functionMap.put("listDevices", new ListAvailableDevices());
return functionMap;
}
}
Now, as you see above i got some public vars for easier access, those are initiated inside new Initialize() which looks like this:
package com.as3breeze.air;
import android.bluetooth.BluetoothAdapter;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import com.adobe.fre.FREWrongThreadException;
import com.as3breeze.air.BluetoothExtensionContext;
public class Initialize implements FREFunction {
#Override
public FREObject call(FREContext context, FREObject[] args) {
BluetoothExtensionContext bluetoothContext = (BluetoothExtensionContext) context;
bluetoothContext.bluetooth = BluetoothAdapter.getDefaultAdapter();
bluetoothContext.extensionActivity = bluetoothContext.getActivity();
FREObject returnData = null;
if( bluetoothContext.bluetooth == null )
{
try {
returnData = FREObject.newObject("notSupported");
} catch (FREWrongThreadException e) {}
}
return returnData;
}
}
Initiation works fine, i also got other methods listed in the Map such as enabling / disabling bluetooth, discoverability and few more, everything there works well.
But the problem is in this one: functionMap.put("listDevices", new ListAvailableDevices());
The class is created and running and returning, it looks like this:
package com.as3breeze.air;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import com.adobe.fre.FREASErrorException;
import com.adobe.fre.FREArray;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREInvalidObjectException;
import com.adobe.fre.FREObject;
import com.adobe.fre.FRETypeMismatchException;
import com.adobe.fre.FREWrongThreadException;
public class ListAvailableDevices implements FREFunction {
static FREArray returnDevicesArr = null;
#Override
public FREObject call(FREContext context, FREObject[] args) {
BluetoothExtensionContext bluetoothContext = (BluetoothExtensionContext) context;
returnDevicesArr = bluetoothContext.nonBoundedDevices;
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int index = 0;
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice bt = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText( context, "Searching devices...", Toast.LENGTH_SHORT).show();
FREArray deviceName;
try {
deviceName = FREArray.newArray(1);
deviceName.setObjectAt(0, FREObject.newObject(bt.getName()));
deviceName.setObjectAt(1, FREObject.newObject(bt.getAddress()));
returnDevicesArr.setObjectAt(index, deviceName);
index++;
} catch (FREASErrorException e) {
e.printStackTrace();
} catch (FREWrongThreadException e) {
e.printStackTrace();
} catch (FREInvalidObjectException e) {
e.printStackTrace();
} catch (FRETypeMismatchException e) {
e.printStackTrace();
}
}
}
};
bluetoothContext.extensionActivity.registerReceiver(mReceiver, filter);
bluetoothContext.bluetooth.startDiscovery();
return null; // Or to use return returnDeviceArr;
}
}
As you see, im trying to store all found devices in returnDeviceArr either to return from call() or in some "global" variable defined in BluetoothExtensionContext.java, it does not matter which way to go, i just need to get hold of that data.
Im not being able to reach the returnDeviceArr variable from onReceive method. Ive also tested to create a new FREArray inside onReceive and store devices data there, so it can be returned but return null; at bottom of call(){ ... } is fired and ultimately giving me null value.
So, how can i make it possible to replace that return null with return returnDeviceArr; and get the array of available devices ?
Im hoping for code examples and explanations so i can start to understand Android coding without using visual components.
The BlueToothExtension class must extend the activity class. Declaring in the manifest alone is not enough. And you should register your broadcast receiver with the intent. Check out the android development guide for more detail.
For Intents to pass variables, you should put them in extras like
`Intent intent = new Intent(this, target.class);
intent.putExtra("variable", value);
startActivityForResult(intent, request_Code);`
In the target class you can fetch the variable using intent.getStringExtra("variable");