简体   繁体   中英

Unable to resume activity when try to setImage for imageview

Thanks for Attention

i am still stuck when try to set image for image view onresume activity

public class CardActivity extends Activity {

public static class CardType {
    public static String Owner = "owner";
    public static String VIP = "vip";
    public static String Staff = "staff";
    public static String Normal = "white";
}

private static ViewConfiguration vc = null;
private static int SWIPE_MIN_DISTANCE = 120;
private static int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

Context context;
ErrorHandle errHandle = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();

    if (MainActivity.MainPreferences == null) {
        MainActivity.MainPreferences = new AppPreferences(context);
    }
    // Halt all the autosaved function
    MainActivity.MainPreferences.putAutoSavedData(false);

    errHandle = new ErrorHandle(this);

    // Set the screen layout first
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Show the content
    assign_layout();

    setup_Gesture();
}

@Override
public synchronized void onResume() {
    errHandle.showDebugToast("CardActivity.onResume", "Start");
    super.onResume();
    assign_MembershipData();

}
@Override
public synchronized void onPause() {
    super.onPause();
}

@Override
public void onDestroy() {
    errHandle.showDebugToast("CardActivity.onDestroy", "Start");

    super.onDestroy();
}

private void assign_layout() {
    String cardTypeText = MainActivity.MainPreferences.getCardType();
    TextView fullName_text = (TextView) findViewById(R.id.textFullName);
    TextView cardNumber_text = (TextView) findViewById(R.id.textCardNumber);
    try {
        if (cardTypeText.equalsIgnoreCase(CardType.Owner)) {
            setContentView(R.layout.activity_card_owner);
            fullName_text.setTextColor(Color.WHITE);
            cardNumber_text.setTextColor(Color.WHITE);
        } else if (cardTypeText.equalsIgnoreCase(CardType.VIP)) {
            setContentView(R.layout.activity_card_vip);
        } else if (cardTypeText.equalsIgnoreCase(CardType.Staff)) {
            setContentView(R.layout.activity_card_staff);
        }  else {
            setContentView(R.layout.activity_card);
        }
    } catch (Exception e) {
        // TODO e
    }
}

private void assign_MembershipData() {
    errHandle.showDebugToast("CardActivity.assign_MembershipData", "Start");
    Bitmap bitmap = null;
    // Get the message from the intent
    //Intent intent = getIntent();
    String fullName = MainActivity.MainPreferences.getFullName();
    String cardNumber = MainActivity.MainPreferences.getMemberNumber().replace("-", " ");
    String email = MainActivity.MainPreferences.getEmailAddress();

    Typeface font = Typeface.createFromAsset(getAssets(), "cynthe.ttf");  

    TextView fullName_text = (TextView) findViewById(R.id.textFullName);
    fullName_text.setTypeface(font);
    fullName_text.setText(fullName);
    ImageView qr_code_image = (ImageView) findViewById(R.id.qr_code_image_barcode);

    qr_code_image.setBackgroundResource(R.drawable.ic_archi2);
    TextView cardNumber_text = (TextView) findViewById(R.id.textCardNumber);
    cardNumber_text.setTypeface(font);
    cardNumber_text.setText(cardNumber);    }

private void setup_Gesture() {

    // Gesture setup
    vc = ViewConfiguration.get(context);
    SWIPE_MIN_DISTANCE = vc.getScaledTouchSlop();
    SWIPE_THRESHOLD_VELOCITY = vc.getScaledMinimumFlingVelocity();

    // Gesture detection
    gestureDetector = new GestureDetector(this, new CardGestureDetector());
    gestureListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    ImageView imageCard = (ImageView) findViewById(R.id.imageCard);
    imageCard.setClickable(true);
    imageCard.setOnTouchListener(gestureListener);
}

public void close_VirtualCard() {
    Intent intent = new Intent();
    setResult(RESULT_OK, intent);

    // Reactivate all the autosaved function
    MainActivity.MainPreferences.putAutoSavedData(true);

    finish();
}

class CardGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                errHandle.showDebugToast("CardGestureDetector.onFling", "Right to Left");
                close_VirtualCard();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                errHandle.showDebugToast("CardGestureDetector.onFling", "Left to right");
                close_VirtualCard();
            } else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                errHandle.showDebugToast("CardGestureDetector.onFling", "Up to down");
                close_VirtualCard();
            } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                errHandle.showDebugToast("CardGestureDetector.onFling", "down to up");
                close_VirtualCard();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}}

and i get this error

04-17 11:37:15.562: E/AndroidRuntime(15922): FATAL EXCEPTION: main
04-17 11:37:15.562: E/AndroidRuntime(15922): java.lang.RuntimeException: Unable to resume activity {package.name/package.name.CardActivity}: java.lang.NullPointerException
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2613)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2641)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2127)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.os.Looper.loop(Looper.java:137)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.main(ActivityThread.java:4895)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at dalvik.system.NativeStart.main(Native Method)
04-17 11:37:15.562: E/AndroidRuntime(15922): Caused by: java.lang.NullPointerException
04-17 11:37:15.562: E/AndroidRuntime(15922):    at package.name.CardActivity.assign_MembershipData(CardActivity.java:136)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at package.name.CardActivity.onResume(CardActivity.java:84)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.Activity.performResume(Activity.java:5237)
04-17 11:37:15.562: E/AndroidRuntime(15922):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2603)

XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/SplashScreen"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:gravity="fill" >

    <ImageView
        android:id="@+id/imageCard"
        style="@style/SplashScreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/auto_small_text"
        android:scaleType="fitXY"
        android:src="@drawable/member_card_vip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="30dp"
        android:gravity="right"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textFullName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:gravity="right"
            android:padding="0dp"
            android:text="@string/membercard_fullname"
            android:textColor="@color/LightGoldenrodYellow"
            android:textSize="@dimen/vcard_text_normal" />

        <TextView
            android:id="@+id/textCardNumber"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="-15dp"
            android:gravity="right"
            android:padding="0dp"
            android:text="@string/membercard_number"
            android:textColor="@color/LightGoldenrodYellow"
            android:textSize="@dimen/vcard_number_normal" />
    </LinearLayout>

    <ImageView
        android:id="@+id/qr_code_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="17dp"
        android:layout_marginTop="10dp" />

</RelativeLayout>

It is work well in high spec device but i got error when i try to run in the low memory device

Make sure you initialize all your layout after calling setContentView(R.layout.name); method in your assign_layout() method.

Just change your method as below:

private void assign_layout() {
    String cardTypeText = MainActivity.MainPreferences.getCardType();
       try {
        if (cardTypeText.equalsIgnoreCase(CardType.Owner)) {
            setContentView(R.layout.activity_card_owner);
            fullName_text.setTextColor(Color.WHITE);
            cardNumber_text.setTextColor(Color.WHITE);
        } else if (cardTypeText.equalsIgnoreCase(CardType.VIP)) {
            setContentView(R.layout.activity_card_vip);
        } else if (cardTypeText.equalsIgnoreCase(CardType.Staff)) {
            setContentView(R.layout.activity_card_staff);
        }  else {
            setContentView(R.layout.activity_card);
        }
    TextView fullName_text = (TextView) findViewById(R.id.textFullName);
    TextView cardNumber_text = (TextView) findViewById(R.id.textCardNumber);

    } catch (Exception e) {
        // TODO e
    }
}

I would suggest you to initialize your views inside your if loop only because you are inflating different layouts based on condition. So your views will be always initialized based on the layout which is inflated. So remove all the views initialization from assign_MembershipData() and move it into the assign_layout() according to the layouts in which your views resides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM