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]