我目前正在重构我的测试应用程序的登录页面,并且似乎无法弄清楚为什么每次setContentView(R.layout.activity_main_page)执行时我都会遇到此错误。
在" setContentView(R.layout.activity_main_page)"触发com.example.jake.fieldinttest E: FATAL EXCEPTION: main
Process: com.example.jake.fieldinttest, PID: 15597
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jake.fieldinttest/com.example.jake.fieldinttest.LoginPage.LoginPageActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
Caused by: java.lang.IllegalStateException: Fragment com.example.jake.fieldinttest.LoginPage.LoginPageFragment did not create a view.
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3545)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:330)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:414)
at android.app.Activity.setContentView(Activity.java:2414)
at com.example.jake.fieldinttest.LoginPage.LoginPageActivity.onCreate(LoginPageActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我正在重构登录页面以遵循MVP架构,我已成功为我的应用程序的其他页面实现,但是对于初始登录页面,我收到错误。似乎可能是视图在设置之前没有被创建/膨胀,但为什么这会在这里造成问题,而不是在我的应用程序的任何其他页面中?我将此页面建模为与其他文件一样的XML文件。唯一的区别是它是应用程序的第一页。也许明显的问题是发射器页面?
感谢任何帮助,谢谢。
package com.example.jake.fieldinttest.LoginPage;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.example.jake.fieldinttest.R;
import com.example.jake.fieldinttest.TestingModuleRepository;
import com.example.jake.fieldinttest.Util.ActivityUtils;
public class LoginPageActivity extends FragmentActivity {
private LoginPageFragment loginPageFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
//set fragment to activity
loginPageFragment = (LoginPageFragment)
getSupportFragmentManager().findFragmentById(R.id.loginpage_fragment);
if (loginPageFragment == null){
loginPageFragment = new LoginPageFragment();
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), loginPageFragment, R.id.loginpage_fragment);
}
//create the presenter
new LoginPresenter(
TestingModuleRepository.getInstance(),
loginPageFragment);
}
}
package com.example.jake.fieldinttest.LoginPage;
import android.support.annotation.NonNull;
import com.example.jake.fieldinttest.TestingModuleRepository;
public class LoginPresenter implements LoginContract.Presenter {
private final TestingModuleRepository mModuleRepository;
private final LoginContract.View mLoginView;
public LoginPresenter(
@NonNull TestingModuleRepository moduleRepository,
@NonNull LoginContract.View loginView) {
mModuleRepository = moduleRepository;
mLoginView = loginView;
mLoginView.setPresenter(this);
}
@Override
public void start(){}
public void buttonClicked() {}
}
package com.example.jake.fieldinttest.LoginPage;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
public class LoginPageFragment extends Fragment implements
LoginContract.View {
private LoginContract.Presenter mPresenter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public LoginPageFragment(){
Bundle arguments = new Bundle();
}
@Override
public void onResume() {
super.onResume();
mPresenter.start();
}
@Override
public void setPresenter(@NonNull LoginContract.Presenter presenter) {
mPresenter = presenter;
}
@Override
public void displayIncorrectLoginToast() {
}
}
package com.example.jake.fieldinttest.LoginPage;
import com.example.jake.fieldinttest.BasePresenter;
import com.example.jake.fieldinttest.BaseView;
public interface LoginContract {
interface View extends BaseView<Presenter> {
void displayIncorrectLoginToast();
}
interface Presenter extends BasePresenter {
void buttonClicked();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/page_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment class = "com.example.jake.fieldinttest.LoginPage.LoginPageFragment"
android:id="@+id/loginpage_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jake.fieldinttest">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.setup.PLAY_SETUP_SERVICE"/>
<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=".LoginPage.LoginPageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FleetPage.FleetListPageActivity">
<intent-filter>
<action android:name="com.example.jake.fieldinttest.FleetPage.FleetListPageActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainMenuPage.MainMenuActivity">
<intent-filter>
<action android:name="com.example.jake.fieldinttest.MainMenuPage.MainMenuActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".PanelViewPage.PanelViewMain">
<intent-filter>
<action android:name="com.example.jake.fieldinttest.PanelViewPage.PanelViewMain" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapPage.TotalMapViewPage">
<intent-filter>
<action android:name="com.example.jake.fieldinttest.MapPage.TotalMapViewPage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SomeNewPage">
<intent-filter>
<action android:name="com.example.jake.fieldinttest.SomeNewPage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
使用Fragments有两种主要方法,您可以检查here,这些方法是:Activity的布局中的片段或Java代码。似乎你正在使用第一个,片段在Activity的布局XML中声明。
在LogingPagerFragement.java中,您应该覆盖onCreateView,并返回由布局片段填充的View。你的代码看起来像那样:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment,
container, false);
return v; }
虽然我强烈建议采用第二种方法,但这应该可行。