0

My app has two activities: MainActivity and OnlineActivity. The code snippet of onCreate method of MainActivity.java is:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Button btnOnline = (Button)findViewById(R.id.Online);
        if (btnOnline != null){
            btnOnline.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                     online();
                }
            });
        }
    }

The online method inside MainActivity.java is defined as follows:

 private void online() {
        Intent intent = new Intent(packageContext:MainActivity.this, OnlineActivity.class);
        startActivity(intent);
    }

Similarly, code snippet of onCreate method of OnlineActivity.java is:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);}

The activity_main.xml has a button (button id is Online) and the content_main.xml has a simple text view.

While running the app, the button is displayed first, while pressing on the button the app crashes without any error (next view is not displayed).

The Logcat (Verbose) is as follows:

enter image description here

After crashing, the displayed message is My_App keeps stopping. And I get App info and Close app options.

What could have gone wrong in my code?

Thank you.

santobedi
  • 866
  • 3
  • 17
  • 39

2 Answers2

0

In the MainActivity.java Please change the inside online method:

 private void online() {
  try{
        startActivity(new Intent(MainActivity.this,OnlineActivity.class));

     }
  catch (Exception e){
            e.printStackTrace();
     }
}
Pramesh Bhalala
  • 273
  • 1
  • 3
  • 9
0

With the code snippets provided, I don't see any reason why the app should crash. Just for the sanity check, please compare the below snippet with yours: NOTE: below code runs without crash and the ToolBar isn't necessary if you aren't using those. I hope this helps.

MainActivity.java

    public class MainActivity extends AppCompatActivity {

  private AppCompatButton onlineButton;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    onlineButton = findViewById(R.id.Online);

    setOnlineClickListener()`enter code here`;
  }

  private void setOnlineClickListener() {

    onlineButton.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent onlineIntent = new Intent(MainActivity.this, OnlineActivity.class);
        startActivity(onlineIntent);
      }
    });
  }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

  <com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"
      >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

  </com.google.android.material.appbar.AppBarLayout>

  <androidx.appcompat.widget.AppCompatButton
      android:id="@+id/Online"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!"
      android:layout_margin="16dp"
      android:layout_gravity="bottom|center_horizontal"
      />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

OnlineActivity.java

    public class OnlineActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_online);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
  }
}

activity_online.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OnlineActivity"
    >

  <com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"
      >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

  </com.google.android.material.appbar.AppBarLayout>

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="This is Online Activity"
      />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.foo">

  <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/AppTheme">
    <activity
        android:name=".OnlineActivity"
        android:label="@string/title_activity_online"
        android:theme="@style/AppTheme.NoActionBar"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>
theThapa
  • 581
  • 4
  • 11