Reputation: 91
I try to make my StartActivity
class can dynamically change from one Fragment
to another, such as first it will open up LoginFragment
. If user does not have any account they could register new account. Login and register would be two different fragments in a single activity.
Here's the StartAcitivity.class
public class StartActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.flDetailContainer, new LoginFragment(), "LOGIN")
.commit();
}
}
}
My LoginFragment.class
public class LoginFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_login, parent, false);
}
}
And the activity_start.xml
layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/flDetailContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</FrameLayout>
My fragment_login.xml
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
And the error I got.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.carclubapps, PID: 2748
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.carclubapps/com.example.asus.carclubapps.StartActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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 do have RegistrationFragment and fragment_registration, but the thing is on my StartActivity I only call for LoginFragment not RegistrationFragment yet. Am I missing something here?
RegistrationFragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Asus on 11/27/2016.
*/
public class RegistrationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_registration, parent, false);
}
}
fragment_registration
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_registration"/>
</LinearLayout>
Upvotes: 0
Views: 355
Reputation: 10635
From Logs its clear in your RegistrationFragment you are trying to access some view which actually not exist in your attached layout file of fragment.
So confirm in your RegistrationFragment that you attached correct view. Because generally this error(View not found) comes when you trying to use view which actually not exist with current attached view.
If still not works then try to invalidate cache and restart you Android Studio.
Upvotes: 0
Reputation: 2203
the error is coming from another Activity, Mainly the Registerfragment. The view is incorrect.
Unable to start activity --> No view found for fragment RegistrationFragment
If you fix the view and the problem is persistant, kindly update us and we will work it out promptly.
Upvotes: 0
Reputation: 24251
The logcat clearly indicates that the problem is in your RegistrationFragment
in the activity_start.xml
layout.
No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
It also says you're referring to an invalid view which doesn't really exists in your activity_start.xml
layout.
Here's your activity_start.xml
layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/flDetailContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</FrameLayout>
I see one and only FrameLayout
with id flDetailContainer
which you might not want to get from your RegisterFragment
. I think you're getting wrong view inside your RegisterFragment
where you're getting the view from layout using LayoutInflater
.
I guess, there's a layout called fragment_register.xml
. You need to get the view from that layout in your RegisterFragment
.
In that case your onCreateView
function of the RegisterFragment
may look like
public class RegisterFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_register, parent, false);
}
}
Upvotes: 1