一个java.lang.NullPointerException,而我不';我不知道是什么';这是不对的
我一直在寻找可能对我有帮助的答案,但所有的解决方案都会给我同样的错误。 我对java语言非常陌生,所以一些建议将非常有用 这是错误一个java.lang.NullPointerException,而我不';我不知道是什么';这是不对的,java,android,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 referenc
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)
这是主要的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>
线,但我不知道是什么。
任何带有解决方案的一般性建议都会非常有用。
谢谢 当
setContentView
中指定的布局不正确时,此错误很常见。重新检查是否指定了包含下一行按钮的正确布局:
setContentView(R.layout.activity_main);
当
setContentView
中指定的布局不正确时,此错误很常见。重新检查是否指定了包含下一行按钮的正确布局:
setContentView(R.layout.activity_main);
我敢打赌,如果调试代码,您将看到
findViewById(R.id.uploadImg)
生成null。现在需要找出原因。重新检查这一行setContentView(R.layout.activity_main)中是否使用了正确的布局名称代码>@AjilO。天哪,这就是解决办法!我引用了一个不同的xml,而不是主xml。将此作为答案发布?我将把它标记为已解决。谢谢你,伙计@希尔米巴哈,非常欢迎你。我已经被这类事情困扰了好几天了。我敢打赌,如果你调试代码,你会看到findViewById(R.id.uploadImg)
产生空值。现在需要找出原因。重新检查这一行setContentView(R.layout.activity_main)中是否使用了正确的布局名称代码>@AjilO。天哪,这就是解决办法!我引用了一个不同的xml,而不是主xml。将此作为答案发布?我将把它标记为已解决。谢谢你,伙计@希尔米巴哈,非常欢迎你。我已经被这类事情缠住好几天了