Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FindViewBydLayoutFlator无法膨胀我的布局,使用时返回null_Java_Android_Xml_Layout - Fatal编程技术网

Java FindViewBydLayoutFlator无法膨胀我的布局,使用时返回null

Java FindViewBydLayoutFlator无法膨胀我的布局,使用时返回null,java,android,xml,layout,Java,Android,Xml,Layout,我的主要活动要求view膨胀main.xml,但无论什么原因似乎总是返回null,我在这里有点迷茫,因为它在4.2中起作用,但现在在将源代码的其余部分升级到6.0之后就没有了 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = (LayoutInflater) this.ge

我的主要活动要求view膨胀main.xml,但无论什么原因似乎总是返回null,我在这里有点迷茫,因为它在4.2中起作用,但现在在将源代码的其余部分升级到6.0之后就没有了

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.main, null);

    setContentView(layout);

    ctx = this;

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    res = getResources();

    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    mMediaButtonReceiver = new RemoteControlReceiver();
    mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    mediaFilter.setPriority(Integer.MAX_VALUE);

    registerReceiver(mMediaButtonReceiver, mediaFilter);

    //mDrawable = (this.res.getDrawable(R.drawable.glowy_metal));
    //mBackground.setBackgroundDrawable(mDrawable);
    mBackground = (ImageView) layout.findViewById(R.id.background);
    mBackground.setOnTouchListener(new backgroundTouchListener());
logcat在此处显示空值:

mBackground = (ImageView) layout.findViewById(R.id.background);
mymain.xml中的背景id

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitCenter"
        android:background="@drawable/glowy_metal"/>
只用

setContentView(R.layout.main);
后来

mBackground = (ImageView) findViewById(R.id.background);
无需创建新的充气机,因为
活动已经有了您想要的方法。

只需使用

setContentView(R.layout.main);
后来

mBackground = (ImageView) findViewById(R.id.background);

无需创建新的充气机,因为
活动
已经有方法来执行您想要的操作。

这就是我最初使用它的方式,但它仍然给我空值,并在打开应用程序时使其崩溃。logcat在mBackground=(ImageView)layout.findViewById(R.id.background)处显示null;不管我如何设置iTunes,更新你的问题,并包括崩溃的输出,以便我们可以帮助查看出了什么问题。这是在用我这里的代码更新代码之后吗?这并不重要,但请注意,
findViewById(..)
没有使用
layout
视图。根据API级别,您没有该布局的自定义版本,是吗?尝试将布局强制转换为
RelativeLayout
,并对其调用
getChildCount()
,查看它是否有一个子级。或者调用
((RelativeLayout)layout).getChildAt(0)
查看它是否是您的ImageView@cphelps76你有没有发现问题出在这里?这就是我最初使用它的方式,但它仍然给我空值,并在打开应用程序时崩溃。logcat在mBackground=(ImageView)layout.findViewById(R.id.background)处显示null;不管我如何设置iTunes,更新你的问题,并包括崩溃的输出,以便我们可以帮助查看出了什么问题。这是在用我这里的代码更新代码之后吗?这并不重要,但请注意,
findViewById(..)
没有使用
layout
视图。根据API级别,您没有该布局的自定义版本,是吗?尝试将布局强制转换为
RelativeLayout
,并对其调用
getChildCount()
,查看它是否有一个子级。或者调用
((RelativeLayout)layout).getChildAt(0)
查看它是否是您的ImageView@cphelps76你有没有发现这里有什么问题?