gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:40:25 25 4
gpt4 key购买 nike

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

这是错误,我不知道出了什么问题,该示例来自 2011 年,因此它可能包含与 android 4.4 不兼容的内容。

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

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

最佳答案

您以 null Intent 调用 PendingIntent.getActivity(...) 会导致 NullpointerException。在此示例中,我们将重复使用您的 Main Activity(但您也可以使用 list 中的 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);

关于java - 示例应用程序无法在虚拟设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20846776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com