标签: findviewbyid

findViewById()函数返回null

我有布局文件custom_lock.xml.它有3个子布局.问题是当我尝试从MainActivity.java访问布局的元素时,它返回null.它让我疯狂.请帮我.谢谢.

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp" >

        <TextView
            android:name="@+id/place1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:gravity="center"
            android:text="Place"
            android:textColor="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:name="@+id/listofapps"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light"
            android:text="List of Apps" />

        <ListView
            android:name="@+id/sites"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_green_light" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:name="@+id/calories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_purple"
            android:text="Health Data" />

        <ListView
            android:name="@+id/dialer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以下是我的MainActivity.java

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.custom_lock); …
Run Code Online (Sandbox Code Playgroud)

android findviewbyid

-1
推荐指数
1
解决办法
134
查看次数

带有对话框的findViewbyID

我试图在特定对话框中提取用户输入.但是,当我尝试使用findViewbyID并放入时

final EditText ExceedingLimitInput = (EditText)dialog.findViewbyId(R.id.ExceedingLimitInput);
Run Code Online (Sandbox Code Playgroud)

Android Studio说没有定义对话框.我不太清楚我应该在这做什么.

我已经挖掘了一些与此相关的问题,但是大多数问题只与Fragment的使用有关,而我没有使用任何碎片.也许我正在延长错误的课程?

有人可以说明为什么没有定义对话框,以及我如何解决它?

这是我的代码供参考:

设置菜单(主类)

package com.example.denny.protoype2;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class SettingsMenu extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_menu);
        //Exceeding Limit Dialog
        final Button ExceedingLimitButton = (Button)findViewById(R.id.ExceedingLimitButton);
        ExceedingLimitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Dialog ExceedingLimitDialog = new Dialog(SettingsMenu.this);
                ExceedingLimitDialog.setTitle("Exceeding Limit Dialog");
                ExceedingLimitDialog.setContentView(R.layout.exceeding_limit_dialog);
                ExceedingLimitDialog.show();

                final EditText ExceedingLimitInput = (EditText)dialog.findViewbyId(R.id.ExceedingLimitInput);

            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

android dialog findviewbyid

-1
推荐指数
1
解决办法
397
查看次数

如何使findViewByID全局

晚上好,

我有一个问题,我想一次将“ finViewById”声明为全局。但是它只能用作本地文件,因此我必须复制并粘贴它们,这很糟糕。

公共类MainActivity扩展了AppCompatActivity {

//(I WANT HERE)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //(IT ONLY works HERE)
    Button mygtukas = (Button) findViewById(R.id.pgrButton);
    mygtukas.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView laukelis = (TextView) findViewById(R.id.pgrTekst);
                    laukelis.setText("Bamm, trumpas");
                }
            }
    );
    mygtukas.setOnLongClickListener(
            new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                //(I DON'T WANT THIS COPY as well)
              TextView laukelis =(TextView)findViewById(R.id.pgrTekst);
                    laukelis.setText("Bamm, ilgas");
                    return true;
                }
            }
    );
}
Run Code Online (Sandbox Code Playgroud)

android global-variables local findviewbyid

-1
推荐指数
1
解决办法
585
查看次数

多按钮初始化

我有10个按钮,我想通过调用一个方法来初始化它们。我的意思是现在我有很多:

button= (Button) findViewById(R.id.button);
button1= (Button) findViewById(R.id.button1);
Run Code Online (Sandbox Code Playgroud)

我想要一个这样的方法,并在每次按钮初始化时调用它:

private void initialize(Button mybutton){
  mybutton= (Button) findViewById(R.id.mybutton);
}
Run Code Online (Sandbox Code Playgroud)

android button findviewbyid

-1
推荐指数
1
解决办法
125
查看次数

标签 统计

android ×4

findviewbyid ×4

button ×1

dialog ×1

global-variables ×1

local ×1