Christian Heinisch Christian Heinisch - 2 months ago 17
Android Question

java.lang.IllegalArgumentException: No view found for id 0x7f0c007b

I tried to start a App project and now i get the following error.

I tried a lot from the answers that i found here on the side, but I don't found my mistake.

Here is the output:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.christian_heinisch.tiercheckliste, PID: 28372
java.lang.IllegalArgumentException: No view found for id 0x7f0c007b (de.christian_heinisch.tiercheckliste:id/relativeLayout_for_About) for fragment AboutFragment{5abcffb #0 id=0x7f0c007b}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 28372 SIG: 9
Application terminated.


Here is my MainActivity

package de.christian_heinisch.tiercheckliste;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
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.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

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

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_pet_about) {

} else if (id == R.id.nav_pet_info) {

} else if (id == R.id.nav_pet_checklist) {

} else if (id == R.id.nav_info) {

AboutFragment aboutFragment = new AboutFragment();
FragmentTransaction manager = getSupportFragmentManager().beginTransaction();
manager.replace(R.id.relativeLayout_for_About, aboutFragment);
manager.addToBackStack(null);
manager.commit();

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}


Here is my fragment_about.xml

<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="de.christian_heinisch.tiercheckliste.AboutFragment"
android:id="@+id/relativeLayout_for_About">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</RelativeLayout>


And here is my AboutFragment.java

package de.christian_heinisch.tiercheckliste;


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


/**
* A simple {@link Fragment} subclass.
*/
public class AboutFragment extends Fragment {


public AboutFragment() {
// Required empty public constructor
}


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

}


Does anyone have an idea what is wrong?

Answer

A FragmentTransaction replaces a View within your Activity hierarchy with your Fragment. The View with the ID R.id.relativeLayout_for_About is in the layout your are using for your Fragment, not the layout of the Activity. Add a View in your R.layout.activity_main where you want your AboutFragment to appear:

<FrameLayout android:id="@+id/placeholder_about"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Then replace this View with your AboutFragment:

Fragment fragment = new AboutFragment();

getSupportFragmentManager().beginTransaction()
        .replace(R.id.placeholder_about, fragment).addToBackStack(null).commit();

You can read more about this paradigm in the Developer Training docs.