Intent method under OnClickListener

Sunday, May 4, 2014

I am having a weird problem with my program in eclipse. I have multiple activity and my intent methods jumps to one activity to another. It was working to other activities but for a certain activity it doesn't work, hence, app crashes. I have the following activity intent hop:


GameMenu intent buttons direct to > AppInfo & InstructionScreen (works fine) AppInfo intent button directs back to > GameMenu (works fine) Instruction intent button directs to > ColorGame activity (crashes).


Here is my code for the InstructionScreen:



public class InstructionScreen extends Activity {
ViewFlipper c2s;
ImageView c2s1,c2s2;
MediaPlayer buttonClick;
Intent playGame;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instruction_screen);
c2s = (ViewFlipper) findViewById (R.id.click2start);
buttonClick = MediaPlayer.create(this, R.raw.buttonclick);
c2s.setFlipInterval(100);
c2s.startFlipping();
this.clickToStart();
}

private void clickToStart() {
c2s1 = (ImageView) findViewById (R.id.c2s1);
c2s2 = (ImageView) findViewById (R.id.c2s2);
c2s1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
buttonClick.start();
c2s.stopFlipping();
playGame = new Intent(InstructionScreen.this, ColorGame.class);
playGame.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(playGame);

}
});

c2s2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
buttonClick.start();
c2s.stopFlipping();
playGame = new Intent(InstructionScreen.this, ColorGame.class);
playGame.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(playGame);

}
});

}


}


Here is the code for ColorGame: (the content is not done, but i doubt that it was the problem)



public class ColorGame extends Activity {
TextView timer, score;
ViewFlipper colors;
ImageView yellow, red, green;
MediaPlayer backgroundMusic;
int gameTime, points;
Random colorNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_game);
backgroundMusic = MediaPlayer.create(this, R.raw.colorgamebgs);
backgroundMusic.start();

yellow = (ImageView) findViewById(R.id.yellowbar);
red = (ImageView) findViewById(R.id.redbar);
green = (ImageView) findViewById(R.id.greenbar);

colorNum = new Random();

timer = (TextView) findViewById(R.id.timer);
timer.setText("START");

this.startGame();
}


private void startGame() {
// TODO Auto-generated method stub

}


}


Also, here is the logcat:



05-03 20:46:12.479: E/SpannableStringBuilder(28535): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 05-03 20:46:12.479: E/SpannableStringBuilder(28535): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 05-03 20:46:19.594: E/dalvikvm-heap(28535): Out of memory on a 1200016-byte allocation. 05-03 20:46:19.634: E/AndroidRuntime(28535): FATAL EXCEPTION: main 05-03 20:46:19.634: E/AndroidRuntime(28535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ics136.colorgame/com.ics136.colorgame.ColorGame}: android.view.InflateException: Binary XML file line #42: Error inflating class 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread.access$700(ActivityThread.java:140) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.os.Handler.dispatchMessage(Handler.java:99) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.os.Looper.loop(Looper.java:137) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread.main(ActivityThread.java:4938) 05-03 20:46:19.634: E/AndroidRuntime(28535): at java.lang.reflect.Method.invokeNative(Native Method) 05-03 20:46:19.634: E/AndroidRuntime(28535): at java.lang.reflect.Method.invoke(Method.java:511) 05-03 20:46:19.634: E/AndroidRuntime(28535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 05-03 20:46:19.634: E/AndroidRuntime(28535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 05-03 20:46:19.634: E/AndroidRuntime(28535): at dalvik.system.NativeStart.main(Native Method) 05-03 20:46:19.634: E/AndroidRuntime(28535): Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 05-03 20:46:19.634: E/AndroidRuntime(28535): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 05-03 20:46:19.634: E/AndroidRuntime(28535): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.Activity.setContentView(Activity.java:1924) 05-03 20:46:19.634: E/AndroidRuntime(28535): at com.ics136.colorgame.ColorGame.onCreate(ColorGame.java:22) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.Activity.performCreate(Activity.java:5188) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 05-03 20:46:19.634: E/AndroidRuntime(28535): ... 11 more 05-03 20:46:19.634: E/AndroidRuntime(28535): Caused by: java.lang.reflect.InvocationTargetException 05-03 20:46:19.634: E/AndroidRuntime(28535): at java.lang.reflect.Constructor.constructNative(Native Method) 05-03 20:46:19.634: E/AndroidRuntime(28535): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.view.LayoutInflater.createView(LayoutInflater.java:593) 05-03 20:46:19.634: E/AndroidRuntime(28535): ... 24 more 05-03 20:46:19.634: E/AndroidRuntime(28535): Caused by: java.lang.OutOfMemoryError 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:636) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:484) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:813) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.content.res.Resources.loadDrawable(Resources.java:2277) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.widget.ImageView.(ImageView.java:126) 05-03 20:46:19.634: E/AndroidRuntime(28535): at android.widget.ImageView.(ImageView.java:116) 05-03 20:46:19.634: E/AndroidRuntime(28535): ... 27 more








http://ift.tt/1iXn6SF