【问题标题】:Accessing button view of an inflated layout访问膨胀布局的按钮视图
【发布时间】:2019-12-12 01:24:26
【问题描述】:

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

我一直在处理的代码是:

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

【问题讨论】:

  • 使用vw.findViewById(...);setOnClickListener

标签: android android-layout


【解决方案1】:

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

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

【讨论】:

  • 我试过了,一点击按钮应用就崩溃了。
  • 取消注释kill()函数。
  • 即使在那之后应用程序崩溃了。我无法解决这个问题。
  • 我重启了adb服务器,错误以FATAL EXCEPTION: main;开头
  • 问题是你将kill()函数设置为onClick。但没有kill()。取消注释kill()
【解决方案2】:

您只需要在膨胀视图中按 id 找到按钮。

LayoutInflater li=getLayoutInflater();
vw=li.inflate(R.layout.frag, null);
btn = (Button)vw.findViewById(R.id.btn);

【讨论】:

  • 我已经在 getid() 方法中通过 id 找到了按钮。当我为此按钮调用事件侦听器方法时,应用程序崩溃。我错过了什么吗?
【解决方案3】:

我迟到了,但仍然发布答案,认为这可能会对我有所帮助,因为我是新手,而且我的投票没有显示出来,并且不允许使用 cmets。

我在 MainActivity 中对包含 Button 的布局进行了充气,​​但无法获得充气按钮的功能。然后我完全应用了@CaseyB的方法,感谢上帝。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多