我正在尝试让一个图像按钮 OnClick 实时创建另一个动态 ImageButton 到一个单独活动的布局,但是新按钮创建的布局不断变为 null 并出现 nullpointerexception 错误并且应用程序崩溃,我在做什么错误的?
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);
}}}
我希望它出现的活动
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));
}
}
}
感谢您的耐心我很新我已经将主类编辑到我现在正在尝试的内容,我刚刚创建了一个 XML ImageButton 并分配了一个要调用的 ID,而不是即时进行并设置它的参数。虽然仍然没有运气:/
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]