1

我正在学习 Android,当它们处于不同的风格时,我找不到片段实例化错误的解决方案。

设置:

  • 我有一个加载到主要活动中的主要片段。
  • 我有两个好处:付费和免费。
  • 应用程序的主要(公共)部分没有片段(布局 + java 文件),我把它们放在了有利位置。

当我尝试运行我的程序时出现以下错误:

01-24 19:38:06.211 27417-27417/com.udacity.gradle.builditbigger.paid E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.udacity.gradle.builditbigger.paid, PID: 27417
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.udacity.gradle.builditbigger.paid/com.udacity.gradle.builditbigger.MainActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
Caused by: java.lang.NullPointerException
    at java.lang.VMClassLoader.findLoadedClass(Native Method)
    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:499)
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3637)
    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:780)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.udacity.gradle.builditbigger.MainActivity.onCreate(MainActivity.java:14)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我尝试了许多不同版本的片段名称,因为错误消息告诉我问题出在此,不幸的是它们都不起作用。你能帮我找出问题的原因吗(我的意思不仅仅是解决方案,而且为什么这不起作用)?代码可以在 GitHub 上找到,但相关文件在这里:

src/java/com/udacity/gradle/builditbigger/MainActivity.java

package com.udacity.gradle.builditbigger;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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);
}

public void tellJoke(View view) {
    new EndpointsAsyncTask().execute(this);
}
}

src/res/layout/activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_main" />

src/free/java/com/udacity/gradle/builditbigger/free/MainActivityFragment.java

package com.udacity.gradle.builditbigger.free;

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

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.udacity.gradle.builditbigger.R;


/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_main, container, false);

    AdView mAdView = (AdView) root.findViewById(R.id.adView);
    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device. e.g.
    // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mAdView.loadAd(adRequest);
    return root;
}
}

src/free/res/layout/fragment_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.udacity.gradle.builditbigger.MainActivity">

<TextView
    android:id="@+id/instructions_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/instructions" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/instructions_text_view"
    android:onClick="tellJoke"
    android:text="@string/button_text" />

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

</RelativeLayout>

src/paid/java/com/udacity/gradle/builditbigger/paid/MainActivityFragment.java

package com.udacity.gradle.builditbigger.paid;

import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.udacity.gradle.builditbigger.R;


/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_main, container, false);
    return root;
}
}

src/paid/res/layout/fragment_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.udacity.gradle.builditbigger.MainActivity">

<TextView
    android:id="@+id/instructions_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/instructions" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/instructions_text_view"
    android:onClick="tellJoke"
    android:text="@string/button_text" />

</RelativeLayout>
4

1 回答 1

4

同时,我找到了解决方案。src/res/layout/activity_main.xml您需要将属性赋予片段以android:name使其工作。两种风格的名称相同 ( MainActivityFragment),但名称空间不同 (freepaid)。解决此问题的唯一方法是您需要将此布局文件移动到应用程序的风味部分,并从主要部分中删除原始文件。长话短说,您必须删除该src/res/layout/activity_main.xml文件,并将这两个文件添加到项目中:

src/free/res/layout/activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.udacity.gradle.builditbigger.free.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_main" />

src/paid/res/layout/activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.udacity.gradle.builditbigger.paid.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_main" />

我很抱歉最初的问题,但我希望它对某人有用。

于 2018-01-24T19:52:20.397 回答