在Android studio中,我得到一个空指针异常,我不知道为什么

时间:2018-06-01 18:02:03

标签: android xml nullpointerexception

在Android Studio中,我得到一个空指针异常,我似乎无法找出原因。我试图在一些文本上面添加一个图像,即使我在xml文件和我的类中都正确标记它,它仍然无法正常工作。

以下是我在该课程中发生问题的代码:

    public class MoreInfoActivity extends AppCompatActivity {
    ImageButton moreInfoImage;
    TextView moreInfoText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_info);


        moreInfoImage.findViewById(R.id.moreInfoPicture);
        moreInfoText = findViewById(R.id.more_info_text);


        final ArrayList<HealthCenterButton> healthCentersList = HealthCenterButton.getHealthCentersFromFile("healthCenters.json", this);
        final ArrayList <String> miniList = new ArrayList <String>();


        for (HealthCenterButton healthCenterButton : healthCentersList) {
            miniList.add(healthCenterButton.mini);
           System.out.println("BEEEP BEEP BEEP BOOP BOP BEEPED BOOP BEEP");
            System.out.println(miniList);
       }



       //set up array for mini iamges







        /*
        ImageView iw= (ImageView)findViewById(R.id.imageView1);

         */


        //make it scrollable
        moreInfoText.setMovementMethod(new ScrollingMovementMethod());

       // int index = getIntent(index);

        int index = getIntent().getExtras().getInt("index");

        //String intentStringNumber = getIntent().getStringExtra("index");

    //        int index = Integer.parseInt(intentStringNumber);

      // int index = 0;

        System.out.println(index +" BOOOP BOP BOOP BOOP BOPP BOOP");

        if (index == 0) {
            //Bowdoin

            moreInfoText.setText("Founded in Boston's Dorchester neighborhood in 1972 by community residents, " +
                    "Bowdoin Street Health Center remains focused on providing outstanding medical care while" +
                    " maintaining a tradition of working with residents to identify and meet their " +
                    "unique health needs. The mission of the health center is to provide excellent, " +
                    "compassionate care to our patients and support the health of the entire community.");
           // moreInfoImage =



        }

        else  if (index == 3) {
            //BIDMC Chelsea

            moreInfoText.setText("In the midst of a thriving Chelsea neighborhood, " +
                    "we bring world-class health care to patients north of Boston. " +
                    "Our patients have the best of all worlds: dedicated staff, the atmosphere of a private practice, " +
                    "and access to the expertise, resources, and systems of Beth Israel Deaconess Medical Center.");


        }

        else  if (index == 2) {
            //BIDMC
            moreInfoText.setText("Beth Israel Deaconess Medical Center (BIDMC) in Boston, Massachusetts is a teaching hospital of Harvard Medical School. It was formed out of the 1996 merger of Beth Israel Hospital (founded in 1916) and New England Deaconess Hospital (founded in 1896). Among independent teaching hospitals, Beth Israel Deaconess Medical Center consistently ranks in the top three recipients of biomedical research funding from the National Institutes of Health. Research funding totals nearly $200 million annually. BIDMC researchers run more than 850 active sponsored projects and 200 clinical trials. " +
                    "The Harvard-Thorndike General Clinical Research Center, " +
                    "the oldest clinical research laboratory in the United States, " +
                    "has been located on this site since 1973");

        }

        else  if (index == 1) {
            //Dimock Community Health Center Complex
            moreInfoText.setText("Recognized nationally as a model for the delivery of comprehensive health and human services in an urban community, The Dimock Center provides the residents of Boston with convenient access to high quality, low cost health care and human services that might not otherwise be available to the communities we serve. " +
                    " In 2013, Dimock handled over 76,000 visits annually with 17,000 unique patients and " +
                    "clients representing a wide range of socioeconomic and ethnic backgrounds.");

        }

        else  if (index == 4) {
            //Fenway Health
            moreInfoText.setText("For more than forty years, " +
                    "Fenway Health has been working to make life healthier for the people in our neighborhood, " +
                    "the LGBT community, people living with HIV/AIDS and the broader population. Fenway was " +
                    "founded in 1971 as part of the free clinic movement by students who believed that" +
                    " ''health care should be a right, not a privilege. In its early days, " +
                    "Fenway was a drop-in clinic providing free blood pressure checks and STD screenings. Over the years, Fenway obtained permanent space and incorporated as a freestanding health center with a staff of one volunteer doctor, one nurse and one intake worker. Today, Fenway Health has a budget of more than $90 million, a staff of 500 and a patient population of more than 28,000. " +
                    "Fenway Health is a Federally Qualified Community Health Center.");

        }

        else  if (index == 5) {
            //South Cove Medical Center

            moreInfoText.setText("South Cove Community Health Center ('South Cove') is the premier Asian community health center of Massachusetts. Founded in 1972, South Cove provides medical and behavioral health services and youth and family programs for approximately 32,000 clients at four locations in the greater Boston area. Our mission is to improve the health and well being of all medically underserved in Massachusetts, with a special focus on Asian Americans.  " +
                    "This mission is accomplished by providing high quality, " +
                    "community-based health care and health promotion programs " +
                    "that are accessible and linguistically and culturally competent for these populations.");

        }

        else  if (index == 6) {
            //South Cove Chinatown
            moreInfoText.setText("South Cove Community Health Center ('South Cove') is the" +
                    " premier Asian community health center of Massachusetts. Founded in 1972, South Cove provides medical and" +
                    " behavioral health services and youth and family programs for approximately 32,000 clients at four locations in the greater Boston area. " +
                    "Our mission is to improve the health and well being of all medically underserved in Massachusetts, with a special focus on Asian Americans. " +
                    " This mission is accomplished by providing high quality," +
                    " community-based health care and health promotion programs" +
                    " that are accessible and " +
                    "linguistically and culturally competent for these populations");

        }




    }
    }

这是我的XML文件的代码:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/more_info_text"
        android:layout_width="match_parent"
        android:layout_below="@id/moreInfoPicture"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        />

    <ImageButton
        android:id="@+id/moreInfoPicture"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:padding="0dp"

        />




</RelativeLayout>
    </ScrollView>

这是我得到的错误

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.leo.ConnexusHealth/com.example.leoconnelly.connexus.MoreInfoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageButton.findViewById(int)' on a null object reference

1 个答案:

答案 0 :(得分:1)

只是删除这一行:

moreInfoImage.findViewById(R.id.moreInfoPicture);