示例应用程序不会在虚拟设备上运行

时间:2013-12-30 20:12:48

标签: java android android-lifecycle

我有android生命周期示例,我可以在具有android 2.1-update1的物理设备上运行,但它不会在虚拟设备android 4.4上运行。

这是错误,我不知道有什么问题,这个例子来自2011年所以它可能包含与android 4.4不兼容的东西。

这是代码。还有2个额外的短班,但我认为这里不需要它们。

  

清单

<?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>
  

抽象活动类

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);
      }
   }
}
  

主要

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();
   }   
}

1 个答案:

答案 0 :(得分:1)

您使用null意图调用PendingIntent.getActivity(...)会导致您的Nullpointerexception。对于此示例,我们将重新使用您的Main活动(但您也可以在清单中使用Activity2或Activity3。)

改变:

notification.contentIntent = PendingIntent.getActivity(this, 0, null, 0);

到:

final Intent mainIntent = new Intent(mContext, Main.class);
notification.contentIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);