感谢关注
我在尝试为图像视图设置图像时仍然卡住了恢复活动
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;
}
}}
我收到此错误
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>
它在高规格设备中运行良好,但是当我尝试在低内存设备中运行时出现错误
答案 0 :(得分:0)
确保在setContentView(R.layout.name);
方法中调用assign_layout()
方法后初始化所有布局。
只需按以下方式更改您的方法:
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
}
}
我建议您仅在if
循环内初始化视图,因为您根据条件对不同的布局进行了充气。因此,您的视图将始终根据夸大的布局进行初始化。因此,请从assign_MembershipData()
中删除所有视图初始化,并根据视图所在的布局将其移至assign_layout()
。