从布局文件

时间:2017-09-05 00:27:35

标签: android xml listview

所以当我偶然发现这个时,我正在研究一些基础知识。我正在尝试实现listview(将其布局存储在文件eachrow.XML中)和我的应用程序上的一个按钮,这样当用户单击一个按钮“schoollife”时,listview会自动导入存储在其他文件“eachrow2.XML”中的布局。
eachrow.xml的布局仅由按钮组成,而eachrow2.xml的布局由按钮组成 一切似乎工作正常,用户单击按钮导入其他布局,但问题在用户尝试单击第二个布局中的按钮时启动。在错误日志中,它声明按钮'subject'导致空指针异常

eachrow.xml: enter image description here

eachrow2.xml:enter image description here

===========码========
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()));

        }


    }
}

1 个答案:

答案 0 :(得分:0)

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

subjectbutton位于eachrow2.xml中,因此您只需要从该文件中查找activity_main.xml。