I have been trying to implement parse.com push notifications on my iOS and android app. Ive successfully been able to implement this on iOS, however, I'm still struggling with the Android implementation. When the notification is received by the user, and the user presses the notification, i want to open up my 'HomeScreen' activity, and display the text from the notification in an alert Dialogue. the Receiver handler and manifest files are shown below, along with the LogCat. What am i doing wrong, and how do i fix it?
HomeScreen Java:
public class Receiver extends ParsePushBroadcastReceiver {
public Receiver(){
}
private static final String TAG = "MyNotificationsReceiver";
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, HomeScreen.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
String notificationText = json.getString("alert");
AlertDialog Alert= new AlertDialog.Builder(context)
.setTitle("News")
.setMessage(notificationText)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
})
.show();
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
Manifest:
<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" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="au.gov.nsw.shellharbour.saferroadsshellharbour" />
</intent-filter>
</receiver>
<receiver android:name="au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver" android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
Logcat:
java.lang.RuntimeException: Unable to instantiate receiver au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2260)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
EDIT: Since Receiver is an inner class, it cannot exist outside of an instance of its outer class. This is why you cannot instantiate it via the manifest definition. To solve, mark Receiver as static, or define it as its own class file. (You can probably remove the empty constructor I had previously advised).
java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
You need a constructor so the receiver can be instantiated:
public Receiver(){
}
Related
I'm very new to this and I've been looking around the forum for a while to get an answer to my question but I can't seem to get this working and I would very much appreciate help from your pros :)
As the title specifies I would like to go from activity A to activity B on a button click and from activity B on a button click go back to activity A.
My problem is first of all that I can't even go from activity A to activity B because the app crashes when I press the button. Why?
Second I would like to know if my code for returning from activity B to activity A is correct.
My code to go from activity A to activity B
public Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ToCV(View v) {
Intent intent = new Intent(this, CV.class);
startActivity(intent);
}
}
And now the code for returning from Activity B to Activity A
public class CV extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cv);
}
public void GoBack(View v) {
startActivity(new Intent(CV.this,MainActivity.class));
}
}
here is the crash log when i run the app##
2021-03-03 11:29:46.290 4104-4104/com.example.cvapp D/AndroidRuntime: Shutting down VM
2021-03-03 11:29:46.291 4104-4104/com.example.cvapp W/m.example.cvap: Accessing hidden method Lcom/msic/qarth/PatchStore;->createDisableExceptionQarthFile(Ljava/lang/Throwable;)Z (blacklist, JNI)
2021-03-03 11:29:46.291 4104-4104/com.example.cvapp E/m.example.cvap: [qarth_debug:] get PatchStore::createDisableExceptionQarthFile method fail.
2021-03-03 11:29:46.296 4104-4104/com.example.cvapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cvapp, PID: 4104
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6659)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:6631)
at android.view.View.access$3100(View.java:790)
at android.view.View$PerformClick.run(View.java:26187)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6659)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:6631)
at android.view.View.access$3100(View.java:790)
at android.view.View$PerformClick.run(View.java:26187)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.cvapp/com.example.cvapp.CV}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2112)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1757)
at android.app.Activity.startActivityForResult(Activity.java:4803)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:4743)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:5164)
at android.app.Activity.startActivity(Activity.java:5132)
at com.example.cvapp.MainActivity.ToCV(MainActivity.java:21)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6659)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:6631)
at android.view.View.access$3100(View.java:790)
at android.view.View$PerformClick.run(View.java:26187)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
The manifest
`<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.CVapp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Register your Activity B in the manifest file
Put these lines in your maifest
<activity android:name=".CV"/>
You should register Activity B in your AndroidManifest.xml
Your manifest should look like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.CVapp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CV"/>
</application>
And also if you want to return from Activity B to Activity A, you can call method ->
finish() - in Activity B and that will destroy Activity B and return to previous activity and that's Activity A.
I'm a beginner at Android and Java and I have an error when trying to change an other activity's TextView. I've created MainActivity, and another one (PlayerNames).
I've set PlayerNames to be the Launcher activity, maybe the error is that, I don't know. I post the error and the code:
FATAL EXCEPTION: main
Process: com.example.android.mapamundi, PID: 1423
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.mapamundi/com.example.android.mapamundi.PlayerNames}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.example.android.mapamundi.PlayerNames.<init>(PlayerNames.java:12)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
MAINACTIVITY.JAVA
Well, on MainActivity I only cast the TextViews with public static TextView tv1, so I don't post it
PLAYERNAMES.JAVA
public class PlayerNames extends AppCompatActivity implements View.OnClickListener {
Button accept = (Button) findViewById(R.id.changeNames);
EditText playerInput1 = (EditText) findViewById(R.id.PlayerNameInput1);
EditText playerInput2 = (EditText) findViewById(R.id.PlayerNameInput2);
EditText playerInput3 = (EditText) findViewById(R.id.PlayerNameInput3);
EditText playerInput4 = (EditText) findViewById(R.id.PlayerNameInput4);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_names);
accept.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case (R.id.changeNames):
MainActivity.playerName1.setText(playerInput1.toString());
MainActivity.playerName2.setText(playerInput2.toString());
MainActivity.playerName3.setText(playerInput3.toString());
MainActivity.playerName4.setText(playerInput4.toString());
Intent intent = new Intent(PlayerNames.this, MainActivity.class);
startActivity(intent);
}
}
}
I post AndroidManifest.xml, just in case I have an error or something that causes the error
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.android.mapamundi.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".PlayerNames">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Can someone please tell me what am I doing wrong? TKS!!
You are calling findViewById() to early, before your activity is fully created. It cannot be in a field initialiser. It has to be in (or after) onCreate(). And also after setContentView(), because you first have to set the content of your activity, before you can try to find views inside of it.
I will start from my approach I want to have one service which is running even if none of application screen is visible for user. This service will scan for beacons.
Every screen of application needs to get access to method of service so I used binding to service here.
I designed serviceconnector class which will be connecting Activities with service, this class look like this.
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class ServiceConnector {
Context context;
BeaconScanningService scanningService;
boolean mBound = false;
public ServiceConnector(Context context) {
LogShower.printLogs("Service Connector created");
this.context = context;
createBinding();
}
public void createBinding()
{
Intent intent = new Intent(context, BeaconScanningService.class);
if(scanningService.isBeaconScanningServiceRunning())
{
LogShower.printLogs("Service is already running.");
context.bindService(intent, mConnection, 0);
}
else
{
LogShower.printLogs("Service is not running yet.");
context.startService(new Intent(context, BeaconScanningService.class));
context.bindService(intent, mConnection, 0);
}
}
public void startScanning()
{
LogShower.printLogs("Start Scanning.");
scanningService.startBeaconScanner();
}
public void stopScanning()
{
LogShower.printLogs("Stop Scanning.");
scanningService.stopBeaconScanner();
}
public void destroyBinding()
{
if (mBound) {
scanningService.unbindService(mConnection);
mBound = false;
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service)
{
LogShower.printLogs("onServiceConnected");
// We've bound to LocalService, cast the IBinder and get LocalService instance
BeaconScanningService.LocalBinder binder = (BeaconScanningService.LocalBinder) service;
scanningService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
LogShower.printLogs("onServiceDisconnected");
mBound = false;
}
};
}
This is activity when I make binding and try to unbind
import android.app.Activity;
import android.os.Bundle;
public class StartScreen extends Activity
{
ServiceConnector serviceConnector ;
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.starting_screen_layout);
serviceConnector = new ServiceConnector(this);
}
#Override
protected void onDestroy ()
{
serviceConnector.destroyBinding();
super.onDestroy();
}
}
Name of service is BeaconScanningService and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bka.tog.ole" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ZooBeacon"
android:label="#string/app_name" >
</activity>
<service android:enabled="true" android:name=".beaconeserviceandconnector.BeaconScanningService">
</service>
<activity
android:name=".StartScreen"
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>
The problem is that I don't know why my ServiceConnector class is interprete as Service during unbinding process or just I can't make analysis of this logcat.
3405-3405/com.bka.tog.ole E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bka.tog.ole, PID: 3405
java.lang.RuntimeException: Unable to destroy activity {com.bka.tog.ole/com.bka.tog.ole.StartScreen}: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector$1#41d2f8d0
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3647)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
at android.app.ActivityThread.access$1400(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector$1#41d2f8d0
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:973)
at android.app.ContextImpl.unbindService(ContextImpl.java:1671)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:536)
at com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector.destroyBinding(ServiceConnector.java:59)
at com.bka.tog.ole.StartScreen.onDestroy(StartScreen.java:27)
at android.app.Activity.performDestroy(Activity.java:5412)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1118)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3634)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
at android.app.ActivityThread.access$1400(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
What is the reason of this behavior?
In my ServiceConnector class should be:
context.unbindService(mConnection);
instead of this line:
scanningService.unbindService(mConnection);
I don't know how this misscoding come from, but I lost few hours, so be sure that you unbind from context of activity.
Normally i would delete this question, but one person make it favorite so I want to show what was the reason of my error.
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'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