简体   繁体   中英

ANDROID:starting new activities from button click

I am new to android development so help me for :

I have an xml file with 7 buttons, named as: btn_1 btn_2, ... btn_7

Here is the activity_main.xml

<Button 
   android:layout_width="103dp" 
   android:layout_height="82dp" 
   android:id="@+id/btn_1" 
   android:layout_below="@+id/heading1" 
   android:layout_alignParentLeft="true" 
   android:layout_alignParentStart="true" 
   android:background="@mipmap/pic1" 
   style="@style/Base.Widget.AppCompat.Button.Borderless"
   android:clickable="true" />

AND here is the MainActivity.java -

public class MainActivity extends AppCompatActivity {

  private static Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;

  @
  Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    OnClickButtonListener();
  }

  public void OnClickButtonListener() {
    btn1 = (Button) findViewById(R.id.btn_1);
    btn1.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_1 = new Intent(MainActivity.this, MainActivity_1.class);
        startActivity(intent_1);
      }
    });


    btn2 = (Button) findViewById(R.id.btn_2);
    btn2.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_2 = new Intent(MainActivity.this, MainActivity_2.class);
        startActivity(intent_2);
      }
    });

    btn3 = (Button) findViewById(R.id.btn_3);
    btn3.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_3 = new Intent(MainActivity.this, MainActivity_3.class);
        startActivity(intent_3);
      }
    });

    btn4 = (Button) findViewById(R.id.btn_4);
    btn4.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_4 = new Intent(MainActivity.this, MainActivity_4.class);
        startActivity(intent_4);
      }
    });

    btn5 = (Button) findViewById(R.id.btn_5);
    btn5.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_5 = new Intent(MainActivity.this, MainActivity_5.class);
        startActivity(intent_5);
      }
    });

    btn6 = (Button) findViewById(R.id.btn_6);
    btn6.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_6 = new Intent(MainActivity.this, MainActivity_6.class);
        startActivity(intent_6);
      }
    });

    btn7 = (Button) findViewById(R.id.btn_7);
    btn7.setOnClickListener(new View.OnClickListener() {@
      Override
      public void onClick(View v) {
        Intent intent_7 = new Intent(MainActivity.this, MainActivity_7.class);
        startActivity(intent_7);
      }
    });
  }
}

When I click on the buttons, app crashes, but surprisingly NOT always. Also, in one attempt it crashes on clicking btn_6, then in the next attempt (when I reopen the app), it would crash on pressing a different button, say now button 5!

Now if i close the app, and try again, it may crash on pressing btn_3, while it would have worked perferctly fine in the previous attempt.

Any insights? Thanks.

Here is the most recent logCat:

05-25 23:43:17.139 17042-17042 <XX> W/ <YY>: VFY: unable to resolve virtual method 134: Landroid/app/Activity;.onEnterAnimationComplete ()V
05-25 23:43:17.139 17042-17042 <XX> D/ <YY>: VFY: replacing opcode 0x6f at 0x140b
05-25 23:43:17.179 17042-17042 <XX> D/ <YY>: GC_FOR_ALLOC freed 345K, 3% free 54952K/56516K, paused 15ms, total 16ms
05-25 23:43:17.179 17042-17042 <XX> I/ <YY>-heap: Forcing collection of SoftReferences for 11040016-byte allocation
05-25 23:43:17.199 17042-17042 <XX> D/ <YY>: GC_BEFORE_OOM freed 3K, 3% free 54948K/56516K, paused 22ms, total 22ms
05-25 23:43:17.199 17042-17042 <XX> E/ <YY>-heap: Out of memory on a 11040016-byte allocation.
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: "main" prio=5 tid=1 RUNNABLE
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:   | group="main" sCount=0 dsCount=0 obj=0x41e4f578 self=0x41e3a4e8
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:   | sysTid=17042 nice=-16 sched=0/0 cgrp=apps handle=1074368508
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:   | state=R schedstat=( 3391699451 331158977 2372 ) utm=269 stm=70 core=0
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.content.res.Resources.loadDrawable(Resources.java:2988)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.view.View.<init>(View.java:3563)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.widget.TextView.<init>(TextView.java:881)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>:     at android.widget.Button.<init>(Button.java:108)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:58)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at abcpqr.red.MainActivity_1.onCreate(MainActivity_1.java:21)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.Activity.performCreate(Activity.java:5372)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.os.Looper.loop(Looper.java:176)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at android.app.ActivityThread.main(ActivityThread.java:5419)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at java.lang.reflect.Method.invoke(Method.java:525)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-25 23:43:17.209 17042-17042 <XX> I/ <YY>:     at dalvik.system.NativeStart.main(Native Method)
05-25 23:43:17.209 17042-17042 <XX> D/skia: --- decoder->decode returned false
05-25 23:43:17.209 17042-17042 <XX> D/AndroidRuntime: Shutting down VM
05-25 23:43:17.209 17042-17042 <XX> W/ <YY>: threadid=1: thread exiting with uncaught exception (group=0x41e4e700)
05-25 23:43:17.209 17042-17042 <XX> E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                           java.lang.OutOfMemoryError
                                                                                               at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                               at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
                                                                                               at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                                               at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
                                                                                               at android.content.res.Resources.loadDrawable(Resources.java:2988)
                                                                                               at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                                               at android.view.View.<init>(View.java:3563)
                                                                                               at android.widget.TextView.<init>(TextView.java:881)
                                                                                               at android.widget.Button.<init>(Button.java:108)
                                                                                               at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
                                                                                               at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:58)
                                                                                               at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
                                                                                               at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                                               at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
                                                                                               at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
                                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                                               at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                                                               at abcpqr.red.MainActivity_1.onCreate(MainActivity_1.java:21)
                                                                                               at android.app.Activity.performCreate(Activity.java:5372)
                                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                                                                                               at android.app.ActivityThread.access$700(ActivityThread.java:159)
                                                                                               t android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                               at android.os.Looper.loop(Looper.java:176)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                               at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                                               at dalvik.system.NativeStart.main(Native Method)
05-25 23:43:25.074 17042-17042 <XX> I/Process: Sending signal. PID: 17042 SIG: 9

here is the gist of my xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" 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="xxx.MainActivity"> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_inside" android:layout_below="@+id/LL_subheading1" android:layout_centerHorizontal="true" android:background="@mipmap/pic_small_inside" style="@style/Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_on_the_side1" android:layout_below="@+id/LL_subheading2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="@mipmap/pic_small_on_the_side1" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_on_the_side3" android:layout_below="@+id/LL_subheading2" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:background="@mipmap/pic_small_on_the_side3" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_on_the_side2" android:layout_alignTop="@+id/btn_on_the_side1" android:layout_centerHorizontal="true" android:background="@mipmap/pic_small_on_the_side2" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_outside1" android:layout_below="@+id/LL_subheading3" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="@mipmap/pic_small_outside1" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_outside2" android:layout_below="@+id/LL_subheading3" android:layout_centerHorizontal="true" android:background="@mipmap/pic_small_outside2" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> <Button android:layout_width="103dp" android:layout_height="82dp" android:id="@+id/btn_outside3" android:layout_below="@+id/LL_subheading3" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:background="@mipmap/pic_small_outside3" style="@style/Base.Widget.AppCompat.Button.Borderless" android:clickable="true"/> </RelativeLayout> 

Please use the below code and check

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
        btn6 = (Button) findViewById(R.id.btn_6);
        btn7 = (Button) findViewById(R.id.btn_7);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn_1:
                Intent intent_1 = new Intent(MainActivity.this, MainActivity_1.class);
                startActivity(intent_1);
                break;
            case R.id.btn_2:
                Intent intent_2 = new Intent(MainActivity.this, MainActivity_2.class);
                startActivity(intent_2);
                break;
            ...

            case R.id.btn_7:
                Intent intent_7 = new Intent(MainActivity.this, MainActivity_7.class);
                startActivity(intent_7);
                break;
        }
    }
}

First of all, let me give you some advice. Rearrange your code, to avoid wasting processing time on the creation of the screen (OnCreate method) on the following way:

The design (xml) buttons should have the attribute onclick, like:

<Button android:id="@+id/button" 
    android:onClick="onClickButton" 
    android:text="Awesome" />

Then create a method for each button:

public void onClickButton(View v){
   // Code to run after button click
} 

This method needs to return void and have as argument a view.

To help you with your error, I need the logcat that appears on the android monitor. You just need to connect your phone to the pc and run the app from you developing environment.

You use Big Images and the device runs out of memory. Please check your Images Size. And Share your Xml Layout please

You are getting an OutOfMemoryException . See your

logcatjava.lang.OutOfMemoryError at android.graphics.BitmapFactory. nativeDecodeAsset(Native Method)

The problem is you are creating New activities on your button click activities are heavy plus you are loading bitmaps in those activities from asset folder. New activity instances with heavy Bitmaps in it is causing error.

Solution: replace activities with fragment and use Glide or Picasso to load Images.

Note: From now on if App crashes try to search for "Fatal" in your logcat, that log will have explanation of error.

The errors you are getting are might be due to the

1.use of heavy sized images

2.you should always finish your previous activity to release some memory after moving from one activity to other.

3.Whenever you start to use more number of images in your app you should set android:largeHeap="true" in your Android Manifest file for application

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