Issue
I have android life-cycle example that I can run on my physical device which has android 2.1-update1
, but it won't run on virtual device android 4.4
.
This is the error, I have no idea what is wrong with that, the example is from 2011 so it may contain something incompatible with android 4.4.
Here is the code. There are 2 additional short classes, but I don't think they are needed here.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manning.aip.lifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".Main"
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=".Activity2" />
<activity android:name=".Activity3" />
</application>
</manifest>
Abstract Activity class
public abstract class LifecycleActivity extends Activity {
private static final String LOG_TAG = "LifecycleExplorer";
private NotificationManager notifyMgr;
// default this to true, or use the ctor, to send notifications
// with many events notifications can be slow, but useful to "see" what's happening
private boolean enableNotifications = true;
private final String className;
public LifecycleActivity() {
super();
this.className = this.getClass().getName();
}
public LifecycleActivity(final boolean enableNotifications) {
this();
this.enableNotifications = enableNotifications;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
debugEvent("onCreate");
}
@Override
protected void onStart() {
debugEvent("onStart");
super.onStart();
}
@Override
protected void onResume() {
debugEvent("onResume");
super.onResume();
}
@Override
protected void onPause() {
debugEvent("onPause");
super.onPause();
}
@Override
protected void onStop() {
debugEvent("onStop");
super.onStop();
}
@Override
protected void onDestroy() {
debugEvent("onDestroy");
super.onDestroy();
}
//
// state related
//
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
debugEvent("onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
debugEvent("onSaveInstanceState");
super.onSaveInstanceState(outState);
}
//
// configuration related
//
@Override
public void onConfigurationChanged(Configuration newConfig) {
debugEvent("onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public Object onRetainNonConfigurationInstance() {
debugEvent("onRetainNonConfigurationInstance");
return super.onRetainNonConfigurationInstance();
}
//
// other handy Activity methods
//
@Override
public boolean isFinishing() {
debugEvent("isFinishing");
return super.isFinishing();
}
@Override
public void finish() {
super.finish();
}
@Override
public void onLowMemory() {
Toast.makeText(this, "onLowMemory", Toast.LENGTH_SHORT).show();
super.onLowMemory();
}
//
// notify helper
//
private void debugEvent(final String method) {
long ts = System.currentTimeMillis();
Log.d(LOG_TAG, " *** " + method + " " + className + " " + ts);
if (enableNotifications) {
Notification notification = new Notification(android.R.drawable.star_big_on, "Lifeycle Event: " + method, 0L);
RemoteViews notificationContentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
notification.contentView = notificationContentView;
notification.contentIntent = PendingIntent.getActivity(this, 0, null, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationContentView.setImageViewResource(R.id.image, android.R.drawable.btn_star);
notificationContentView.setTextViewText(R.id.lifecycle_class, getClass().getName());
notificationContentView.setTextViewText(R.id.lifecycle_method, method);
notificationContentView.setTextColor(R.id.lifecycle_method, R.color.black);
notificationContentView.setTextViewText(R.id.lifecycle_timestamp, Long.toString(ts));
notifyMgr.notify((int) System.currentTimeMillis(), notification);
}
}
}
Main
public class Main extends LifecycleActivity {
private Button finish;
private Button activity2;
private Chronometer chrono;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finish = (Button) findViewById(R.id.finishButton);
finish.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
activity2 = (Button) findViewById(R.id.activity2Button);
activity2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Activity2.class));
}
});
chrono = (Chronometer) findViewById(R.id.chronometer);
}
@Override
protected void onResume() {
super.onResume();
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
@Override
protected void onPause() {
chrono.stop();
super.onPause();
}
}
Solution
your call to PendingIntent.getActivity(...)
with a null intent is causing your Nullpointerexception. For this example we'll re-use your Main activity (but you can also use Activity2 or Activity3 as in your manifest).
change :
notification.contentIntent = PendingIntent.getActivity(this, 0, null, 0);
to :
final Intent mainIntent = new Intent(mContext, Main.class);
notification.contentIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
Answered By - petey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.