【问题标题】:button onclick from a layout file布局文件中的按钮 onclick
【发布时间】:2017-09-05 00:27:35
【问题描述】:

所以当我偶然发现这个时,我正在研究一些 android 的基础知识。我正在尝试在我的应用程序上实现一个列表视图(其布局存储在文件 eachrow.XML 中)和一个按钮,这样当用户单击一个按钮“schoollife”时,列表视图会自动导入从其他文件“eachrow2.XML”存储的布局.
eachrow.xml 的布局仅由按钮组成,而 eachrow2.xml 的布局由按钮组成。
一切似乎都工作正常,用户单击按钮会导入另一个布局,但是当用户尝试从 second 布局中单击按钮时,问题就开始了。在错误日志中,它指出 按钮“主题”导致空指针异常

每个行.xml:

eachrow2.xml:

===========代码========
eachrow2.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="horizontal">

        <Button
            android:id="@+id/subjectbutton"

            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:padding="1dp"
            android:layout_margin="1dp"

            android:textAllCaps="false"
            android:background="@color/colorPrimary"
            android:textColor="@color/white"
            />
        <TextView
            android:id="@+id/marksbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:textSize="20dp"
            android:text="click the button to show marks of each subject"
            android:textAlignment="center"

    />


</LinearLayout>

mainactivity.java:
包 com.example.ansh.reportcard;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ListView l;
    private String[] d_myself;
    private ArrayAdapter adp;
    private HashMap<String,String> d_school;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        l=(ListView)findViewById(R.id.a_list);
        d_myself= new String[]{"something"
        };
        d_school=new HashMap<>();
        d_school.put("A","B");



        Button B_myself= (Button)findViewById(R.id.myself);
        B_myself.setOnClickListener(this);


        Button B_school=(Button)findViewById(R.id.school);
        B_school.setOnClickListener(this);

        Button subject=(Button)findViewById(R.id.subjectbutton);
        subject.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.myself){
            adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
            l.setAdapter(adp);
        }
        if (view.getId()==R.id.school){
            adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
            l.setAdapter(adp);
        }
        if (view.getId()==R.id.subjectbutton){
            Button  tmp=(Button)view;
            CharSequence x=tmp.getText();

            TextView t= (TextView) findViewById(R.id.marksbox);
            t.setText(d_school.get(x.toString()));

        }


    }
}

【问题讨论】:

  • 请添加错误日志
  • 我对这个问题和/或错误比其他任何事情都感到困惑。

标签: android xml listview


【解决方案1】:

根据我对您的问题的理解,您从 xml 文件 activity_main.xml 中声明了 Button,而应该从 eachrow2.xml 文件中找到它。

主题按钮位于eachrow2.xml 中,因此您只需从该文件中查找视图,而不是activity_main.xml。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-10
    • 2021-08-06
    • 2012-08-01
    • 2015-11-22
    • 2021-07-29
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多