一个java.lang.NullPointerException,我不知道出了什么问题

时间:2017-07-24 02:38:26

标签: java android

我已经搜索了可能对我有帮助的答案,但所有解决方案仍然给我同样的错误。 我是java语言的新手,所以一些建议会非常有用。

这是错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at hilmyzizi.yuk.MainActivity.onCreate(MainActivity.java:77)

这是 Main_Activity.java

private ViewPager mViewPager;
    ImageView viewImg;
    Button imgUpload;
    private static final int PICK_IMAGE = 100;
    Uri imageUri;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        viewImg = (ImageView)findViewById(R.id.imageView);
        imgUpload = (Button)findViewById(R.id.uploadImg);

        imgUpload.setOnClickListener(new View.OnClickListener(){
           @Override
            public void onClick(View v){
               openGallery();
            }
        });
    }



    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(resultCode==RESULT_OK && requestCode==PICK_IMAGE){
            imageUri=data.getData();
            viewImg.setImageURI(imageUri);
        }
    }

这是 xml文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
    android:layout_width="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="hilmyzizi.yuk.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/report"
        android:textSize="35sp"
        android:layout_marginTop="40dp"
        android:shadowColor="@color/colorPrimaryDark"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="3"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:orientation="vertical"
        android:weightSum="1"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reporter"
            android:textSize="20sp"
            android:layout_marginTop="47dp"
            android:shadowColor="@color/colorPrimaryDark"
            android:shadowDx="2"
            android:shadowDy="2"
            android:shadowRadius="3"
            android:layout_alignParentTop="false"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.90" />

        <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:hint="Name"
            android:inputType="textPersonName"
            android:layout_marginBottom="20dp"/>
    </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/phone"
                android:hint="Phone Number"
                android:inputType="textShortMessage"
                android:layout_marginBottom="20dp"/>
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:layout_marginBottom="20dp"/>
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/location"
                android:hint="Location"
                android:inputType="textShortMessage"
                android:layout_marginBottom="20dp"/>
        </android.support.design.widget.TextInputLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_weight="0.85" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/upload"
            android:id="@+id/uploadImg"
            android:layout_below="@+id/linearLayout"
            android:layout_centerHorizontal="true" />

    </LinearLayout>

</RelativeLayout>
</ScrollView>

我认为

可能有问题
imgUpload = (Button)findViewById(R.id.uploadImg);
行,但我不知道是什么。 任何一般性建议和解决方案都会非常有用。 谢谢!

1 个答案:

答案 0 :(得分:0)

setContentView中指定的布局不正确时,此错误很常见。如果要指定包含以下行中按钮的正确布局,请重新检查:

setContentView(R.layout.activity_main);