标签: findviewbyid

findViewById(int)在Android 3.1其他版本中的一个特定按钮上返回null

这是我的第一个问题,但我正在试图正确地提问.

由于标题只在一个按钮上说,只在android 3.1中,findViewById(int)返回null.在Android 2.2上测试它可以工作,所有其他按钮都可以在两个Android版本中找到.它在此特定行之前和之后找到按钮.

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonInfo = (Button)findViewById(R.id.buttonInfo);//fine
buttonInfo.setOnClickListener(this);
...
Button buttonCallOpScreen = (Button)findViewById(R.id.buttonCallOpScreen);//always returns null in android 3.1
...
Button buttonTakePicture = (Button)findViewById(R.id.buttonTakePictureMain);//fine
buttonTakePicture.setOnClickListener(this);
}
Run Code Online (Sandbox Code Playgroud)

main.xml:

(我知道我不应该使用px它,但它只能在我的设备上工作,因此这种情况很好.)

<Button 
android:id="@+id/buttonInfo" 
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_team" />
...
<Button
android:id="@+id/buttonCallOpScreen"
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_operator" />
...
<Button 
android:id="@+id/buttonTakePictureMain" 
android:text=""
android:layout_width="80px" 
android:layout_height="80px"
android:background="@drawable/btn_take_picture"
android:layout_gravity="bottom|left" />
Run Code Online (Sandbox Code Playgroud)

到目前为止,任何帮助将不胜感激.


回答:

事实证明这不仅仅是丑陋的.由于有几个人在这个项目上工作,似乎在layout-normal/main.xml中更改了一个名称.虽然3.1之前的Android-Versions似乎加载layout/main.xml,但较新的版本使用layout-normal/main.xml.因为如果该按钮的id是不同的findViewById(int)必须返回null,这是可以理解的.

很抱歉打扰你,谢谢你的评论!

由于我的声誉不到100,我无法回答我自己的问题关闭它,如果有人能发布这个答案我会接受它,谢谢.

java xml android findviewbyid

6
推荐指数
1
解决办法
1129
查看次数

Android Activity findviewbyid()为null

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class FileExplorerActivity extends Activity 
{
    public static final String TAG="ricky";
    Button button;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        button = (Button) findViewById(R.id.but);<<<------------------
        if(button == null)
        Log.d(TAG, "FileExplorerActivity: button is null");
    }
    public FileExplorerActivity()
    {
       Log.d(TAG, "FileExplorer: constructor()");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的Activity,由另一个使用Intent的活动引入

Intent openFileBrowser = new Intent(this, FileExplorerActivity.class);
try
{
    startActivity(openFileBrowser); 
}
Run Code Online (Sandbox Code Playgroud)

运行代码后我的LogCat文件说"按钮为空"为什么???

android android-intent findviewbyid

6
推荐指数
1
解决办法
1万
查看次数

使用findViewById()找不到视图

即使ID确实存在,我也无法TextView通过呼叫找到findViewById().

OtherActivity:

public class OtherActivity extends Activity { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = (TextView)findViewById(R.ID.txt02);
        super.onCreate(savedInstanceState);//line 5
        setContentView(R.layout.other_activity);
        //textView.setText(ACCESSIBILITY_SERVICE);
        Button button = (Button)findViewById(R.ID.otherActivity_Btn);
        button.setText(R.string.otherActivityBtn);
        button.setOnClickListener(new GoBackBtnListener(this));
    }

    class GoBackBtnListener implements OnClickListener {
        OtherActivity other = null;
        public GoBackBtnListener(OtherActivity p_other) {
            other = p_other;
        }
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(other, Activity01Activity.class);
            other.startActivity(intent);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在第5行添加了断点,并以调试模式启动了项目.当它在断点处停止时,我将鼠标移动到变量textView,它为空.

other_activity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view findviewbyid

5
推荐指数
1
解决办法
3万
查看次数

View.findViewById()返回null

我有这个代码:

private void inflateMenu() {
    if (menu == null) {
        mFanView = (LinearLayout) findViewById(R.id.fanView);
        final View v = LayoutInflater.from(getContext()).inflate(
                R.layout.sidemenu, mFanView);
        menu = (MenuView) v.findViewById(R.id.listViewMenu);
    }
        menu.setColors(backgroundColor, selectedColor, unselectedColor);
}
Run Code Online (Sandbox Code Playgroud)

调试时,v包含一个id等于的MenuView R.id.listViewMenu + 1.当然这会在menu.setColors上抛出一个NPE.

为什么R.id.listViewMenu包含的ID不会导致XML中带有该ID的视图?

我试图清理我的项目,但它仍然保持不变.

编辑:发布请求的文件.

sidemenu.xml

<com.ui.library.slidingmenu.MenuView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listViewMenu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

编辑:问题解决了.

对于因为findViewById返回null而来到这里的任何人:清理项目.如果它不起作用,请再次清理项目.如果它不起作用,请清除所有项目.直到它工作.如果它不起作用,那么问.

android nullpointerexception findviewbyid

5
推荐指数
1
解决办法
5044
查看次数

调用findViewById()以获取片段视图时出现NullPointerException

我正在尝试设置FragmentStatePagerAdapter一个在我的一个片段上创建一个可滚动的标签界面,类似于这个例子.在我的活动中onCreate:

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

     setVersionString();
     setupActionBar();
     setupNavigationDrawer();
     setupFragments();
}
Run Code Online (Sandbox Code Playgroud)

我将内容视图设置为以下布局(R.layout.base):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BaseActivity" >

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/nav_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFF" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

然后我调用以下函数:

private void setupFragments() {
    // Check that the activity is using the correct layout
    if (findViewById(R.id.main_frame) != null) {
        // Initialize …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments findviewbyid

5
推荐指数
1
解决办法
3803
查看次数

LayoutInflater.inflate 和 findViewById 的区别

获取对像这样的小部件的引用有什么区别:

TableRow row = findViewById(R.id.table_row);
Run Code Online (Sandbox Code Playgroud)

和:

TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
Run Code Online (Sandbox Code Playgroud)

当它TableRow是布局的根或者它只是布局的一小部分时,是否也有区别?

android android-widget layout-inflater findviewbyid

5
推荐指数
1
解决办法
1987
查看次数

findViewById如何工作?

package com.example.dell.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void showToast(View view)
    {
        Toast t= new Toast(this);
        LayoutInflater inflater=getLayoutInflater();
        View v=inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toastViewGroup));
        t.setView(v);
        t.show();


    }
}
Run Code Online (Sandbox Code Playgroud)

从android开发人员网站 findViewById搜索具有给定ID的子视图。

在上面的代码中,谁是父视图,正在为其给定的ID搜索其子级?

android findviewbyid

5
推荐指数
1
解决办法
1万
查看次数

视图绑定对应用程序大小的影响有多大?

文档中指定的View Binding 为每个XML 元素生成一个Binding 类,以便于访问和检查,并减轻findViewById.

虽然findViewById通过更昂贵的关联搜索起作用,但由于映射,绑定应该“直接”访问通过 id 搜索的资源。

这种时间闪电是否对应于空间权重?

拥有使用经典完全开发的大型项目findViewById,无法“在黑暗中”转换为 Binding,我们甚至无法进行实验并冒着损害项目的风险。

所以..

给定绑定,应用程序大小增加了多少?Binding 生成的类对维度的影响有多大?

android findviewbyid android-studio android-viewbinding

5
推荐指数
0
解决办法
134
查看次数

为填充按钮的线条布局添加触摸监听器

我在线性布局中添加了触摸事件以响应滑动手势,并且效果很好.但是,当我向布局添加按钮时,将忽略父线框布局.我应该如何防止这种情况发生?

LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);
Run Code Online (Sandbox Code Playgroud)

我该怎么用onInterceptTouch

button touch-event android-linearlayout swipe-gesture findviewbyid

4
推荐指数
1
解决办法
1万
查看次数

为什么在使用另一个android项目作为库时获得NoSuchFiledException?

我有一个可以自己运行的android项目.我想创建另一个扩展前项目的android项目.但是当库项目尝试创建一些使用findViewById(R.id.something)的组件时,它会抛出NoSuchFieldException.

这是库项目的代码:

/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY …
Run Code Online (Sandbox Code Playgroud)

android findviewbyid nosuchfieldexception

4
推荐指数
1
解决办法
1067
查看次数