-2

尊敬的 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)
4

3 回答 3

3

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

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

还要记住:由于它们是不同的布局,它们可能不包含相同的组件,因此,当您决定要设置哪个布局时,如果您尝试调用按钮和当前布局中没有声明,会抛出异常。

于 2013-04-15T12:09:31.183 回答
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)); 
                    }
                  });
                     }

现在在任何你想要的地方调用这个方法,不要忘记在两个布局中添加按钮布局定义..

于 2013-04-15T12:12:57.190 回答
1

您应该根据包含的布局 First 获取每个视图的所有 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);
            }

假设您必须维护一个Booleanlayout当您使用两者中的任何视图时,两者都存在layout

于 2013-04-15T12:14:29.473 回答