親愛的用戶在stackoverflow我最近添加了一個新的比賽,我的應用程序「(MinePedia)」和贏家收到免費比賽冠軍應用程序插件「(com.shadycorp) 「如果用戶安裝了比賽插件」(com.shadycorp)「,我希望我的應用程序主要活動在運行時顯示備用layout.xml,並且如果嘗試實現我想要的結果並使用它,我的應用程序崩潰,我每次啓動時間it.So可以在這裏請人幫我應用程序崩潰,而試圖改變運行時的主佈局
這裏是(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)
請問是何意圖 – Shayden117
調用findViewById()*之後* setContentView() – etienne
同意@etienne – Rohit