When I choose some images from gallery, application is crashed. I guess I loose activity. Because after choose image, line 77 :mSchoolNumber nullpointer exception is given in InitialiseFields() function. As I researhed, its problem is memory. But I cannot solve it.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_edit_profile);
initializeFields();
initializeListeners();
}
public void initializeFields(){
mSchoolNumber = (EditText) findViewById(R.id.editSchoolNumber);
mUserName = (EditText) findViewById(R.id.editUserName);
mPassword = (EditText) findViewById(R.id.editPassword);
saveInfoButton = (Button)findViewById(R.id.saveInfoButton);
editProfileImageProgresBar = (DonutProgress)findViewById(R.id.editProfileImageProgresBar);
editProfileImageView = (ImageView)findViewById(R.id.editProfileImageView);
imageLoader = ConfigFile.getImageLoader(this);
mainActivityIntent = new Intent(this, MainActivity.class);
mSchoolNumber.setText(UniPromClient.getSchoolNumber().toString());
mUserName.setText(UniPromClient.getUserName().toString());
mPassword.setText(UniPromClient.getPassword().toString());
//showProfileImageOnServer();
}
class ImagePickListener implements OnClickListener {
@Override
public void onClick(View v) {
try{
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//ACTION_GET_CONTENT
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
}
startActivityForResult(Intent.createChooser(intent, "Bir Fotoğraf Seçin"), IMAGE_PICK);
}catch(Exception e){
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_PICK && resultCode == RESULT_OK && null != data && data.getData() != null) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
default:
break;
}
}
}
}
private void imageFromGallery(int resultCode, Intent data) {
try{
pd.show();
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File file = new File(filePath);
cursor.close();
//sendProfileImage(file,filePath);//Bunu kaldırıp dene bir de
}catch(Exception e){
}
}
-------------------- XML File-------------------
<LinearLayout 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"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:gravity="center_horizontal"
android:orientation="vertical"
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="com.unipromapp.uniprom.unipromapp.LoginActivity"
android:background="@drawable/mainbackgorund">
<!-- Login progress -->
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp">
<LinearLayout
android:id="@+id/content_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_profile_layoutshape"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/editProfileImageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/editprofile"
android:layout_centerInParent="true"/>
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="@+id/editProfileImageProgresBar"
custom:donut_background_color="#DDFFFFFF"
custom:donut_finished_stroke_width="4dp"
custom:donut_progress="0"
custom:donut_text_size="20dp"
custom:donut_unfinished_stroke_width="4dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true" />
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/editTextHint"
android:theme="@style/MyTextInputLayout">
<EditText
android:id="@+id/editSchoolNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:hint="@string/userLoginName"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/editText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/editTextHint"
android:theme="@style/MyTextInputLayout">
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/userName"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/editText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/editTextHint"
android:theme="@style/MyTextInputLayout">
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/editText" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/saveInfoButton"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/editprofile_buttonshape"
android:text="@string/saveInfo"
android:textColor="#d1d5d0"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
----------------------------------Application gives this error--------------------
20 21:25:57.783 1627-1627/com.unipromapp.uniprom.unipromapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.unipromapp.uniprom.unipromapp, PID: 1627 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unipromapp.uniprom.unipromapp/com.unipromapp.uniprom.unipromapp.UserEditProfileActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.access$800(ActivityThread.java:142) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5118) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.unipromapp.uniprom.unipromapp.UserEditProfileActivity.initializeFields(UserEditProfileActivity.java:77) at com.unipromapp.uniprom.unipromapp.UserEditProfileActivity.onCreate(UserEditProfileActivity.java:62) at android.app.Activity.performCreate(Activity.java:5275) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.access$800(ActivityThread.java:142) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5118) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method)