开始新片段后活动崩溃
我试图让我的StartActivity
课程可以从一个Fragment
动态更改为另一个,比如首先会打开LoginFragment
。如果用户没有任何账户,他们可以注册新账户。在单个活动中登录和注册将是两个不同的片段。开始新片段后活动崩溃
这里是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();
}
}
}
我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);
}
}
而且activity_start.xml
布局
<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>
我fragment_login.xml
布局
<?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>
而我得到的错误。
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)
我有RegistrationFragment和fragment_registration,但事情是在我的StartActivity我只要求LoginFragment尚未RegistrationFragment。我在这里错过了什么吗?
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>
回答:
logcat清楚地表明问题出现在您的RegistrationFragment
的activity_start.xml
布局中。
No view found for id 0x7f0c006f (com.example.asus.carclubapps:id/activity_start) for fragment RegistrationFragment{85ef24b #0 id=0x7f0c006f}
它还说,你指的是哪个并没有真正存在于你的activity_start.xml
布局无效视图。
这是你的activity_start.xml
布局
<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>
我看到的唯一ID为flDetailContainer
,你可能不希望从你的RegisterFragment
得到FrameLayout
。我认为你在RegisterFragment
内部看到错误的视图,你可以从LayoutInflater
获得布局视图。
我想,这里有一个名为fragment_register.xml
的布局。您需要从RegisterFragment
的布局中获取视图。
在这种情况下,你的RegisterFragment
的onCreateView
功能可能看起来像
public class RegisterFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_register, parent, false);
}
}
回答:
原木其明确在你RegistrationFragment您试图访问某个视图这实际上不是在片段的附加布局文件存在。
因此确认您的注册片段您附加了正确的视图。因为通常这个错误(查看未找到)当您尝试使用视图实际上不存在当前附加视图。
如果仍然无效,请尝试使缓存无效并重新启动您的Android Studio。
回答:
错误是来自另一个活动,主要是Registerfragment到来。该视图不正确。
Unable to start activity --> No view found for fragment RegistrationFragment
如果修复视图和问题是持续性,请更新我们,我们会解决它尽快。
以上是 开始新片段后活动崩溃 的全部内容, 来源链接: utcz.com/qa/260648.html