android - 访问膨胀布局的按钮 View

标签 android android-layout

在主 Activity 中单击按钮后布局会膨胀。膨胀的布局由另一个按钮组成。如何调用此按钮的事件监听器方法?

我一直在处理的代码是:

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>

fragment 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" />

谢谢。

日志:

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

最佳答案

首先:取消注释 kill() 函数。

第二:当Kill() 函数在按钮点击时被调用,你调用getId() 函数。在 getId() 中,您有 ftv=(TextView)vw.findViewById(R.id.ftv);。此行将产生 null,因为您过去更改了 ftv 的 Id。您必须使用新 ID 找到此按钮。

关于android - 访问膨胀布局的按钮 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24541033/

相关文章:

android - 为什么我包含的布局标签返回 null?

android - 如何在自定义 ViewGroup 中正确扩展 XML-Layout-File?

java - 设备重启后检索HashMap

android - CursorAdapter 动态排序

java - 我的 ActionBar 图标不可点击

java - LocationListener、LocationRequest、LocationServices 在实现 Google Maps API 以在 Android Studio 中查找附近地点时导入无效

android - 布局有两个 fragment : one fragment and a fragment with the Google Map

java - Android Studio 如何将按钮设置到中心并在两个按钮之间留出空间?

java - 如何创建多个通知?

android - 如何将 View 内部发生的事情记录为视频? (安卓)