在主活动中单击按钮后,布局会膨胀。膨胀的布局包含另一个按钮。如何为此按钮调用事件侦听器方法?
我一直在研究的代码是:
MainActivity类:
public class MainActivity extends Activity {
Button btn, fbtn;
EditText et;
String m;
LinearLayout ll;
TextView ftv;
View vw;
int c=0;
ArrayList<Button> abtn= new ArrayList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
LayoutInflater li=getLayoutInflater();
vw=li.inflate(R.layout.frag, null);
getid();
m=et.getText().toString();
abtn.add(fbtn);
fbtn.setTag(c);
ftv.setText(m);
ll.addView(vw);
et.setText("");
c=c+1;//c
}
public void getid(){
et=(EditText) findViewById(R.id.et);
ll=(LinearLayout) findViewById(R.id.ll);
ftv=(TextView)vw.findViewById(R.id.ftv);
ftv.setId(c+1);//Is this possible?
vw.setId(c);//c
fbtn=(Button)vw.findViewById(R.id.fbtn);//How do i use this button's onclick method?
}
/*public void kill(View v){
getid();
TextView tvz=new TextView(this);
tvz.setText("Guyi");
tvz.setTextSize(25);
ll.addView(tvz);
}*/
@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;
}
}
main_activity XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/et"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:hint="Enter"
android:ems="10" >
</EditText>
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send"
android:onClick="click" />
</LinearLayout>
Frag XML(夸大的布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/ftv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiya!!" />
<Button
android:id="@+id/fbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="kill" />
谢谢。
logcat的:
07-02 21:46:35.646: E/AndroidRuntime(3746): FATAL EXCEPTION: main
07-02 21:46:35.646: E/AndroidRuntime(3746): Process: com.example.inflated, PID: 3746
07-02 21:46:35.646: E/AndroidRuntime(3746): java.lang.IllegalStateException: Could not execute method of the activity
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.view.View$1.onClick(View.java:3823)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.view.View.performClick(View.java:4438)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.view.View$PerformClick.run(View.java:18422)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.os.Handler.handleCallback(Handler.java:733)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.os.Handler.dispatchMessage(Handler.java:95)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.os.Looper.loop(Looper.java:136)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-02 21:46:35.646: E/AndroidRuntime(3746): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 21:46:35.646: E/AndroidRuntime(3746): at java.lang.reflect.Method.invoke(Method.java:515)
07-02 21:46:35.646: E/AndroidRuntime(3746): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-02 21:46:35.646: E/AndroidRuntime(3746): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-02 21:46:35.646: E/AndroidRuntime(3746): at dalvik.system.NativeStart.main(Native Method)
07-02 21:46:35.646: E/AndroidRuntime(3746): Caused by: java.lang.reflect.InvocationTargetException
07-02 21:46:35.646: E/AndroidRuntime(3746): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 21:46:35.646: E/AndroidRuntime(3746): at java.lang.reflect.Method.invoke(Method.java:515)
07-02 21:46:35.646: E/AndroidRuntime(3746): at android.view.View$1.onClick(View.java:3818)
07-02 21:46:35.646: E/AndroidRuntime(3746): ... 11 more
07-02 21:46:35.646: E/AndroidRuntime(3746): Caused by: java.lang.NullPointerException
07-02 21:46:35.646: E/AndroidRuntime(3746): at com.example.inflated.MainActivity.getid(MainActivity.java:60)
07-02 21:46:35.646: E/AndroidRuntime(3746): at com.example.inflated.MainActivity.kill(MainActivity.java:66)
07-02 21:46:35.646: E/AndroidRuntime(3746): ... 14 more
答案 0 :(得分:0)
您只需在膨胀的视图中按ID找到按钮。
LayoutInflater li=getLayoutInflater();
vw=li.inflate(R.layout.frag, null);
btn = (Button)vw.findViewById(R.id.btn);
答案 1 :(得分:0)
首先:取消注释kill()
功能。
第二:当点击按钮时调用Kill()
函数时,调用getId()
函数。在getId()
中,您有ftv=(TextView)vw.findViewById(R.id.ftv);
。此行将生成null
,因为您过去更改了ftv
的ID。你必须找到这个带有新Id的按钮。
答案 2 :(得分:0)
我已经来不及了,但仍在发布答案,认为这对我是新来的人可能会有所帮助,并且我的投票不会显示并且不允许发表评论。
我在MainActivity中膨胀了包含Button的布局,但无法获得膨胀按钮的功能。然后我精确地应用了@CaseyB的方法,它很有效,谢谢上帝。