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);
行,但我不知道是什麼。 任何一般性建議以及解決方案都會非常有用。 謝謝!
我敢打賭,如果您調試代碼,你會發現'findViewById(R.id.uploadImg)'是生產空。你現在需要找出原因。 – Tavo
如果您在此行'setContentView(R.layout.activity_main);' –
@AjilO中使用正確的佈局名稱,請重新檢查。噢,我的天啊,那就是解決方案!我引用了一個不同的XML,而不是activity_main。發佈這個答案?我會將其標記爲已解決。謝啦! –