2017-07-24 119 views
1

我已经搜索了可能对我有帮助的答案,但所有解决方案仍然给我提供了同样的错误。 我真的很新的Java语言,所以一些建议将是非常有用的。java.lang.NullPointerException,我不知道什么是错的

这是错误

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

我敢打赌,如果您调试代码,你会发现'findViewById(R.id.uploadImg)'是生产空。你现在需要找出原因。 – Tavo

+0

如果您在此行'setContentView(R.layout.activity_main);' –

+0

@AjilO中使用正确的布局名称,请重新检查。噢,我的天啊,那就是解决方案!我引用了一个不同的XML,而不是activity_main。发布这个答案?我会将其标记为已解决。谢啦! –

回答

0

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

setContentView(R.layout.activity_main); 
相关问题