1

Android studio is not able to run the code in my phone by building apk. When i run the code, apk does not get install on my phone but instead a blank white screen flashes and goes in the background. If i manually create the apk and put in my phone, then it is not able to intall. I dont get any install failure error but the installation process is not able to complete. I dont find such problem for any other applications which i made with android studio.


I want create two dynamic fragments in a single activity. I have a single container as relative layout. I have 2 buttons in my main activity as Red and Blue. When i click on Red button, Red fragment should be loaded. And when i click on Blue button, Blue fragment should be loaded in the container.

Code for the same is -

mainactivity.java

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction =
                fragmentManager.beginTransaction();   
        Red r = new Red(); 
        transaction.add(R.id.relativeLayout, r);
        transaction.commit();
    }  
    public void redclick(View v)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction =
                fragmentManager.beginTransaction();

        Red r = new Red();

        transaction.replace(R.id.relativeLayout, r);
        transaction.commit();
    }   
    public void blueclick(View v)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction =
                fragmentManager.beginTransaction();

        Blue b = new Blue(); 
        transaction.replace(R.id.relativeLayout, b);
        transaction.commit();
    }
}

red.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Red extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.red_layout, container, false);
    }

}

blue.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Blue extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.blue_layout, container, false);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.deva.fragment.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="redclick"
        android:text="Red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.236"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.773"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.12"
        android:onClick="blueclick"/>

    <FrameLayout
        android:id="@+id/relativeLayout"
        android:layout_width="244dp"
        android:layout_height="301dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.69">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

i m getting following exception in logcat -

02-21 10:31:58.454 25675-25675/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.deva.fragment, PID: 25675
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deva.fragment/com.example.deva.fragment.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:141)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:136)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                       at com.example.deva.fragment.MainActivity.onCreate(MainActivity.java:14)
                                                       at android.app.Activity.performCreate(Activity.java:5242)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:136) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5113) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
                                                       at dalvik.system.NativeStart.main(Native Method) 
                                                    Caused by: java.lang.RuntimeException: com.example.deva.fragment.MainActivity@42965778 must implement OnFragmentInteractionListener
                                                       at com.example.deva.fragment.Barcelona.onAttach(Barcelona.java:66)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1640)
                                                       at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1896)
                                                       at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3673)
                                                       at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                       at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
                                                       at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
                                                       at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:692)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:400) 
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                                                       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                       at com.example.deva.fragment.MainActivity.onCreate(MainActivity.java:14) 
                                                       at android.app.Activity.performCreate(Activity.java:5242) 
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164) 
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:136) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5113) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
                                                       at dalvik.system.NativeStart.main(Native Method) 

manifest file -

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

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

build.gradle (app) -

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.deva.fragment"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:0.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}
Devashish Prasad
  • 1,227
  • 1
  • 13
  • 25

1 Answers1

1

The error came from the Barcelona Fragment, not Red/Blue

MainActivity@42965778 must implement OnFragmentInteractionListener
   at com.example.deva.fragment.Barcelona.onAttach

I think the code you posted should work fine, but you either need to

  1. Not use the Barcelona Fragment
  2. Do what it says, and implement that interface
  3. Remove the throw new RuntimeException part of that Fragment's onAttach method.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245