0

I'm trying to Make an Image Button OnClick create another dynamic ImageButton in real time to a Layout on a seperate activity, but the layout where the new button creates keeps turning up null with a nullpointerexception error and the app crashes, what am I doing wrong?

 package org.iimed.www;


import android.R.drawable;
import android.R.string;
import android.os.Bundle;

import android.text.Layout;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import android.widget.RelativeLayout.LayoutParams;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import org.iimed.www.Sundayopen;

    public class Penicillins extends Activity implements OnClickListener {






            public void onCreate(Bundle SavedInstanceState){
                 super.onCreate(SavedInstanceState);  
                 setContentView(R.layout.penicillin);
                ImageButton addmed = (ImageButton) findViewById(R.id.addmed);
                 addmed.setOnClickListener(this);






            }









                     public void onClick(View v){
                         switch (v.getId()) {

                         case R.id.addmed:
                             startActivity(new Intent(Penicillins.this,Sundayopen.class));
                        LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                         final RelativeLayout ll = (RelativeLayout)findViewById(R.id.sundayopen);
                         final  ImageButton  ab = (ImageButton)findViewById(R.id.adaba);

                ab.setLayoutParams(param);
                ll.addView(ab);


            }}}

The Activity I want it to appear in

package org.iimed.www;

import org.iimed.www.MainActivity;
import org.iimed.www.Iimedja;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import android.view.View.OnClickListener;
public class Sundayopen extends Activity implements OnClickListener {   

    ImageButton ab;
       @Override
    protected void onCreate(Bundle iimed) {
         super.onCreate(iimed);
         setContentView(R.layout.sundayopen);
         ImageButton ab = new ImageButton(getApplicationContext());
         ImageButton hb1 = (ImageButton) findViewById(R.id.homebutton1);

         ImageButton sbc = (ImageButton) findViewById(R.id.satlidopen);
         ImageButton abb = (ImageButton) findViewById(R.id.abbutton);
         sbc.setOnClickListener(this);
         hb1.setOnClickListener(this);
         abb.setOnClickListener(this);
         ab.setOnClickListener(this);
    }


         public void onClick(View v){
             switch (v.getId()) {
             case R.id.homebutton1:
                startActivity(new Intent(this, MainActivity.class));
                 break;
             case R.id.satlidopen:
                 MediaPlayer media = MediaPlayer.create(Sundayopen.this, R.raw.openlid);
                 media.start();
                startActivity(new Intent(this,Iimedja.class));
                break;
             case R.id.abbutton:

                    startActivity(new Intent(this, ImageTextListViewActivity.class));

             }
         }




        }

Thanks for your patience I'm very new I've edited the main class up top to what I'm trying with now, I just created an XML ImageButton and assigned an ID to be called, rather then making it on the fly and setting it's parameters. Still no luck though :/

Stack
        Thread [<1> main] (Suspended (exception NullPointerException))  
    <VM does not provide monitor information>   
    Penicillins.onClick(View) line: 63  
    ImageButton(View).performClick() line: 4354 
    View$PerformClick.run() line: 17961 
    Handler.handleCallback(Message) line: 725   
    ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 5328    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 1102 
    ZygoteInit.main(String[]) line: 869 
    NativeStart.main(String[]) line: not available [native method]  
ToeKnee
  • 47
  • 1
  • 10

5 Answers5

0

You are not creating object of image button. It should be like

ImageButton ab = new ImageButton(getApplicationContext);
BBdev
  • 4,898
  • 2
  • 31
  • 45
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
0

Initialize ab like this.

ImageButton ab = (ImageButton) findViewById(R.id.ab);
BBdev
  • 4,898
  • 2
  • 31
  • 45
RussVirtuoso
  • 900
  • 1
  • 9
  • 20
0

First initialize take your layout id which reqire

ImageButton ab = (ImageButton)findviewbyid(R.id.btnAb);

OR If you take dynamic ImageButton

ImageButton ab = new ImageButton(this);
darshan
  • 1
  • 3
0

The nullpointer error is because of in your onClick Listener you have not even initialized the ImageButton ab; and you are directly using it. So First initialize it and then set drawable.

    addmed.setOnClickListener(new OnClickListener() {
         int i;
         RelativeLayout il = (RelativeLayout)findViewById(R.id.sundayopen); 
         ImageButton ab= (ImageButton)findViewById(R.id.<buttonID>); 
         public void onClick(View v){
             ab.setId(i);
            ab.getResources().getDrawable(R.drawable.adaba);

            il.addView(ab);
         }});
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

As far as i can see, you are using improper variable declaration in your code. You have declared same ImageButton ab at the global level and at the local level too.

Once you remove the global variable declaration, you will see an error like this:

change the modifier of Image button ab to final.

Similar case goes with the RelativeLayout ll too.

Also, it is better to use LinearLayout in case of dynamic view creation, as you will have more control of their positioning.

Atish Agrawal
  • 2,857
  • 1
  • 23
  • 38
  • How to I change the modifier of ab to final? this is the only place it is ? EDIT I changed the OnClicker to public final – ToeKnee Oct 29 '13 at 06:06
  • can you please print your whole stacktrace here?? The stack trace points exact exceptions which is really easy to undertsand...it may help me – Atish Agrawal Oct 30 '13 at 06:48
  • I am using a new code so I have a seperate xml for the ImageButton I can call an id on, still having no luck, check my question edit for new cod and stack, thanks for your patience. – ToeKnee Oct 30 '13 at 09:29
  • sorry for late reply...as i was a little busy..i will look into your code and reply you very soon..thanks – Atish Agrawal Nov 04 '13 at 13:52
  • No worries, I got it working dynamically without the I.D, but now I'm stuck on how to save the button persistently. I've been trying with shared preferences but no love. – ToeKnee Nov 05 '13 at 00:01