0

Here is my question:

I would like to implement two buttons in one activity. One is calculate BMI, another is go to check time. I wish users could see two buttons in the same activity, also depends on what they want to do. (These functions are not work at the same time, it is seperately. Also, I implement the button return to the home page both in these two activities.)

In the Android emulator, the message comes " app continued stopping."

Following is the MainActivity.java file:

 
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 
 import androidx.appcompat.app.AppCompatActivity;
 
 public class MainActivity extends AppCompatActivity {
 
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
 
         // bt1 = to BMI  btn = TO DateCheck
         Button bt1 = findViewById(R.id.button1);
         Button btn = findViewById(R.id.button);
 
 
 
         bt1.setOnClickListener(v -> {
             // 指定要呼叫的 Activity Class
             Intent newAct = new Intent();
             newAct.setClass(MainActivity.this, BMI.class);
             // 呼叫新的 Activity Class
             startActivity(newAct);
 
 
         });
 
         btn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent newAct2 = new Intent();
                 newAct2.setClass(MainActivity.this,Time_Date.class);
                 startActivity(newAct2);
 
 
             }
         });
 
 
 
 
 
 
 
     }
 
 
 }
Following is the **Manifest.xml file**:
   
        
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapplication">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.MyApplication">
    
    
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity android:name=".Time_Date"/>
            <activity android:name=".BMI" />
    
        </application>
    
    </manifest>
Following is **activity_main.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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:background="#87CEEA">
    
    
        <!-- to BMI -->
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
    
    
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="100dp"
    
            android:layout_marginLeft="100dp"
            android:layout_marginTop="50dp"
            android:layout_marginEnd="100dp"
            android:layout_marginRight="100dp"
    
            android:backgroundTint="@color/pink"
            android:text="@string/to_bmi"
            android:textAlignment="center"
            android:textColor="@color/purple_500"
            android:textStyle="bold"
            android:textSize="16sp"/>
    
        <!--to DateCheck-->
    
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
    
            android:layout_marginStart="100dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="100dp"
    
            android:text="@string/view_datetime"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/purple_500"
            android:backgroundTint="@color/pink"
    
    
    
            />
    </RelativeLayout>

Thanks!

These messages are the stacktrace after I pressed the button that caused the app stopped:



com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
        at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
        at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)
        at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
        at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
        at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)
     Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
        at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
        at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
        at java.io.InputStream.read(InputStream.java:101)
        at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
        at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4) 
        at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4) 
        at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
        at java.lang.Thread.run(Thread.java:764) 
        at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6) 

楊東翰
  • 9
  • 1
  • 5
  • If the emulator says that the app stopped, you should have a stacktrace in "Run". Can you please paste it into the question? Maybe you can find more information there. – Cactusroot Jul 31 '21 at 09:25
  • @Cactusroot I pasted it into the questuion. Thanks! – 楊東翰 Jul 31 '21 at 12:49

2 Answers2

0

For intent use this code:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
0

Take a look at this answer.

The error has nothing to do with your 2 buttons, but rather an emulator error with something that happens in the activity you just started.


Some things I want to note here:

You are mixing start and left as well as right and end together. You should decide on one of them. The difference is explained here.

You can make the code for the intent shorter as noted by mohammadjavads sabagh's answer

Format your code using CTRL/CMD + ALT + L (also works in xml)

Cactusroot
  • 1,019
  • 3
  • 16