-2

I'm New to android app development.I have developed a basic app and found null point error at a particular line and added if(instance!=null){do something} .But the part inside if i.e, do something part is never executed.How can I fix this? So that my app works property

 public class MainActivity extends ActionBarActivity {  
      private Button btn1;
      private ImageView imagetoshow;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);         
         btn1=(Button)findViewById(R.id.button1);          
         imagetoshow=(ImageView)findViewById(R.id.imageView1);        
         if(btn1!=null){        
              btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     imagetoshow.setImageResource(R.drawable.image1);                   
                }
               }); 
          }      
          if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                      .add(R.id.container, new PlaceholderFragment())
                      .commit();
           }
        }           
        @Override
       public boolean onCreateOptionsMenu(Menu menu) {      
         // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             return true;
         }
         return super.onOptionsItemSelected(item);
     }

     /**
      * A placeholder fragment containing a simple view.
      */
     public static class PlaceholderFragment extends Fragment {

         public PlaceholderFragment() {
         }

         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
             return rootView;
         }
        }

     }
BharathRao
  • 1,846
  • 1
  • 18
  • 28
vamshi
  • 47
  • 8

2 Answers2

2

The code isn't being executed because your if (instance != null) statement isn't true. If you want that code to run, and you don't want to have a NullPointerException then initialise whichever variable is null (i.e. give it a value). At that point you can remove the null check as well as it will no longer be needed.

Post your code if you want a more detailed answer.

Eddie Curtis
  • 1,207
  • 8
  • 20
  • What value can I initialize to variable of type Button? – vamshi Jun 21 '14 at 11:05
  • Button button = (Button) findViewById(R.id.buttonId) – Eddie Curtis Jun 21 '14 at 12:02
  • I've provided the code please look through it.By default I'm having my relative layout in fragment_main.xml and linear layout in activity_main.xml.How could I bring linear layout to activy_main.xml? – vamshi Jun 21 '14 at 15:30
1

Make sure you have linked the relevant layout file with your Activity class and you have declared your Activity in "Android Manifest" as well. If this is not the case then send me your code so I can help you track the issue.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11