尝试在运行时更改主布局时应用程序崩溃

时间:2013-04-15 12:05:14

标签: android

亲爱的stackoverflow用户我最近在我的应用程序中添加了一个新的比赛“(MinePedia)”并且获胜者会收到一个免费的比赛获胜者应用程序插件“(com.shadycorp)”并且我希望我的应用程序主要活动显示替代layout.xml在运行时如果用户安装了竞赛插件“(com.shadycorp)”并且我使用了if和else来尝试实现我想要的结果但是我的应用程序每次启动时都会崩溃。所以任何人都可以请帮助我

这是(Minepedia)的主要活动

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)

3 个答案:

答案 0 :(得分:3)

这是因为您从尚不存在的布局中调用按钮(或任何其他UI组件)。

您需要运行条件逻辑并在调用其上的任何组件之前设置布局

另请注意:由于它们是不同的布局,因此它们可能不包含相同的组件,因此,当您决定要设置的布局时,如果您尝试调用,则只调用其上的组件一个按钮,它没有在当前布局中声明,并且将抛出异常。

答案 1 :(得分:1)

你必须使用默认布局           在创建调用时的第一个语句中的行,因为您无法添加没有布局的按钮

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

                 public void addButton(){

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

现在可以在任何地方调用此方法,并且不要忘记在两个布局中添加按钮布局定义。

答案 2 :(得分:1)

您应该根据包含的布局获取每个视图的所有reference_ID。

  

在尝试使用ID查找视图之前,先设置此条件。

boolean installed  =   appInstalledOrNot("com.shadycorp");  
            if(installed)
            {
            //set if action
                setContentView(R.layout.winner_main);
            }
            else
            {
              //set else action
                setContentView(R.layout.activity_main);
            }

可以假设您在使用Boolean

中的任何一个视图时,必须保留一个layout来表明同时存在layout {{1}}