Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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
Android 设置内容视图和清理项目后,FindViewByID(…)返回null_Android_Layout - Fatal编程技术网

Android 设置内容视图和清理项目后,FindViewByID(…)返回null

Android 设置内容视图和清理项目后,FindViewByID(…)返回null,android,layout,Android,Layout,有很多这样的问题,但解决方案对我来说并不奏效 无论如何,我的主要活动有一个按钮,在它的onclick方法中,它将我带到另一个活动ViewPowerActivity。我有一个布局xml文件,名为power_view.xml。里面我有一些布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:

有很多这样的问题,但解决方案对我来说并不奏效

无论如何,我的主要活动有一个按钮,在它的onclick方法中,它将我带到另一个活动ViewPowerActivity。我有一个布局xml文件,名为power_view.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="wrap_content"
android:layout_marginLeft="@dimen/screen_margin"
android:layout_marginRight="@dimen/screen_margin"
android:layout_marginTop="@dimen/screen_margin"
android:orientation="vertical" >
...
上面的findViewById调用返回null


如果我删除setContentView(…)之后的所有代码并简单地返回到那里,它会很好地显示空布局。我设置了ContentView,清理了项目,尝试将power view设置为主要活动,以及各种各样的事情。还可能是什么?

从您的代码中可以清楚地看出,power\u view是一个布局,即.xml。所以R.id.power_视图是不正确的

您似乎想要访问该布局的父视图。然后执行以下操作

您必须为父LinearLayout设置一个id

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/parentLinear"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="@dimen/screen_margin"
     android:layout_marginRight="@dimen/screen_margin"
     android:layout_marginTop="@dimen/screen_margin"
     android:orientation="vertical" >
     ...

如果您想在powerView中使用其他视图,您应该在xml布局中将id设置为该视图,并通过findViewById(R.id.your_视图)将该视图初始化为power_视图。

从您的代码中可以看出,Rpower_视图是一个布局ie.xml。所以R.id.power_视图是不正确的

您似乎想要访问该布局的父视图。然后执行以下操作

您必须为父LinearLayout设置一个id

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/parentLinear"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="@dimen/screen_margin"
     android:layout_marginRight="@dimen/screen_margin"
     android:layout_marginTop="@dimen/screen_margin"
     android:orientation="vertical" >
     ...
如果您想在powerView中使用其他视图,您应该在xml布局中将id设置为该视图,并通过findViewById(R.id.your_视图)将该视图初始化为power_视图。

power_view.xml
公共类ViewPowerActivity扩展了活动{
私人最终静态权限=新权限();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.power_视图);
Power=powers.getPowers().get(0);
视图powerView=(视图组)findViewById(R.id.layout);
...
}
...
}
power\u view.xml
公共类ViewPowerActivity扩展了活动{
私人最终静态权限=新权限();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.power_视图);
Power=powers.getPowers().get(0);
视图powerView=(视图组)findViewById(R.id.layout);
...
}
...
}

它将是R.id.power\u视图。这是xml的一个视图。不是一个XML,你到底想实现什么?你需要这样的视图powerView=findviewbyd(R.id.layout);这将是R.id.power\u视图。这是xml的一个视图。不是一个XML,你到底想实现什么?你需要这样的视图powerView=findviewbyd(R.id.layout);如果这个答案对你有帮助,请接受并投票表决。请让我知道。这太棒了!我引用线性布局本身,然后抓取它的文本视图并用信息填充它们。出现了一些其他错误,但这是朝着正确方向迈出的一步。谢谢如果这个答案对你有帮助,请接受并投票表决。请让我知道。这太棒了!我引用线性布局本身,然后抓取它的文本视图并用信息填充它们。出现了一些其他错误,但这是朝着正确方向迈出的一步。谢谢
     View powerView = findViewById(R.id.parentLinear);
     power_view.xml


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/screen_margin"
    android:layout_marginRight="@dimen/screen_margin"
    android:layout_marginTop="@dimen/screen_margin"
    android:orientation="vertical" >



    public class ViewPowerActivity extends Activity {
        private final static Powers powers=new StubPowers();

        @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.power_view);

            Power power=powers.getPowers().get(0);
            View powerView = (ViewGroup)findViewById(R.id.layout);
            ...
        }
        ...
    }