当主活动中的按钮被点击后,会膨胀一个布局。膨胀的布局由另一个按钮组成。我该如何调用此按钮的事件监听器方法?
我一直在处理的代码是:
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 layout:
<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" />
Thank you.
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
vw.findViewById(...);
和setOnClickListener
。 - mrd abd