繁体   English   中英

示例应用程序将无法在虚拟设备上运行

[英]Example app won't run on virtual device

我有一个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();
   }   
}

您以空意图调用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM