Reputation: 363
I have an android app which starts out at the main activity that contains a navigation view, and a fragment called fragment_home which has my start button. When I hit that button, another activity is started. This works fine in the device emulator, but it also works when I plug my phone in and build directly to it (Pixel 3XL with Android 9).
However, if I manually install the APK and run it with my phone unplugged, the main activity opens up fine and displays the home fragment contents, but when I press the button the app immediately crashes. Here is the code for my
MainActivity.java
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*
* Avoid reloading fragment when device is rotated
*/
if(savedInstanceState == null)
{
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.fragment_home);
}
}
/* Add functionality to the navigation drawer items */
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.home:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
break;
case R.id.history:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HistoryFragment()).commit();
break;
case R.id.settings:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new SettingsFragment()).commit();
break;
case R.id.help:
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HelpFragment()).commit();
break;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
public void buttonClick(View v) {
switch(v.getId()) {
case R.id.startButton:
/* Start the survey activity */
Intent myIntent = new Intent(MainActivity.this, SurveyClass.class);
startActivity(myIntent);
break;
}
}
}
The buttonClick()
function is the one that is run when the button from my home fragment is clicked.
Here is my home fragment XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_home"
android:background="@color/colorPrimary">
<Button
android:id="@+id/startButton"
android:layout_width="256dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:background="@drawable/button_rounded_corners_white"
android:fontFamily="@font/segoeuib"
android:gravity="center"
android:includeFontPadding="false"
android:onClick="buttonClick"
android:text="FIND PRODUCTS"
android:textColor="@color/colorAccent"
android:textSize="26sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="326dp"
android:layout_height="188dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="29dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="18dp"
app:srcCompat="@drawable/logo_white" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true"
android:textColor="@color/white"
android:textSize="24dp"
android:textStyle="bold"
android:text="@string/subtitle" />
</RelativeLayout>
Upvotes: 2
Views: 479
Reputation: 159
This might be android 9 error! Add this line in your Manifest file within application
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
Upvotes: 1
Reputation: 487
Do you work with android support v4 Fragment ? If it's the case you need to use
getSupportFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
instead of
getFragmentManager().beginTransaction().replace(R.id.placeholder_home, new HomeFragment()).commit();
Can you also post the error obtained ?
Upvotes: 1