尊敬的 stackoverflow 用户,我最近在我的应用程序“(MinePedia)”中添加了一个新竞赛,获胜者会收到一个免费的竞赛获胜者应用程序插件“(com.shadycorp)”,我希望我的应用程序主要活动显示备用 layout.xml在运行时,如果用户安装了竞赛插件“(com.shadycorp)”并且我已经使用 if 和 else 来尝试达到我想要的结果,但是每次我启动它时我的应用程序都会崩溃。所以这里的任何人都可以帮忙我
这是(Minepedia)的MainActivity
package com.shadycorp.minecraftrecipebook;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button2 = (Button) findViewById(R.id.button1);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MainActivity2.class));
}
});
Button button9 = (Button) findViewById(R.id.info);
button9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), AppInf.class));
}
});
Button button91 = (Button) findViewById(R.id.dlc);
button91.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DLC.class));
}
});
Button button96 = (Button) findViewById(R.id.help);
button96.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Instruction.class));
}
});
// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.launcher;
CharSequence tickerText = "MinecraftPedia"; // ticker-text
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "MinePedia";
CharSequence contentText = "This is the quick launch button for MinePedia";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
// and this
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
boolean installed = appInstalledOrNot("com.shadycorp");
if(installed)
{
//set if action
setContentView(R.layout.winner_main);
}
else
{
//set else action
setContentView(R.layout.activity_main);
}
}
private boolean appInstalledOrNot(String uri)
{
PackageManager pm = getPackageManager();
boolean app_installed = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}
}
这是 LogCat
04-15 12:01:07.529: E/AndroidRuntime(827): FATAL EXCEPTION: main
04-15 12:01:07.529: E/AndroidRuntime(827): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shadycorp.minecraftrecipebook/com.shadycorp.minecraftrecipebook.MainActivity}: java.lang.NullPointerException
04-15 12:01:07.529: E/AndroidRuntime(827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)