我有这个静态方法:
public static void displayLevelUp(int level, Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_level_coast,
(ViewGroup) findViewById(R.id.toast_layout_root)); // this row
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("This is a custom toast");
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
.show();
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何让第一个findViewById玩得很好,因为它说它是一种非静态方法.我理解为什么会这么说,但必须有一个解决方法?我context进入了这个方法,但无法一起完成它们.
我有一个在互联网上似乎很常见的问题:我创建了一个只有一个片段的活动.这是生成的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
ListView lv = (ListView) findViewById(android.R.id.list);
}
}
Run Code Online (Sandbox Code Playgroud)
在条件之后或之内,我无法使用findViewById获取任何对象:它始终为null.以下是生成的代码的其余部分:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_customer,
container, false);
return rootView;
}
Run Code Online (Sandbox Code Playgroud)
activity_customer.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.axacs.marc.CustomerActivity"
tools:ignore="MergeRootFrame" />
Run Code Online (Sandbox Code Playgroud)
listview实际上是在fragment_customer.xml中:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/new_folder_button"
android:text="" />
Run Code Online (Sandbox Code Playgroud)
有谁知道缺少什么?
我试图测试Android O Developer Preview的第二阶段.在项目创建之后,我只是点击了构建并运行但我没有任何成功.
Android默认生成的代码如下:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Run Code Online (Sandbox Code Playgroud)
发生编译错误.
Error:(18, 37) error: reference to findViewById is ambiguous
both method findViewById(int) in Activity and method
<T>findViewById(int) in AppCompatActivity match
where T is a type-variable:
T extends View declared in method <T>findViewById(int)
Run Code Online (Sandbox Code Playgroud)
帮我!我该如何解决这个错误?
编辑#1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", …Run Code Online (Sandbox Code Playgroud) 我最近将项目从Eclipse移到了Android Studio.我试图让电子邮件功能正常工作,但我收到"findViewById"错误.我也收到"Toast.makeText"的错误.你能帮忙解决这两个错误.我班上的代码如下:
package com.example.ishonours.witsbusapp;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ComplaintsFragment extends Fragment {
//I added
private EditText recipient;
private EditText subject;
private EditText body;
private OnFragmentInteractionListener mListener;
private static final String ARG_SECTION_NUMBER = "5";
public static ComplaintsFragment newInstance(int menuNumber) {
ComplaintsFragment fragment = new ComplaintsFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, menuNumber);
fragment.setArguments(args);
return fragment;
}
public ComplaintsFragment() {
// Required …Run Code Online (Sandbox Code Playgroud) 所以我用过Kotlin Android Extensions,我觉得它很容易使用,非常值得.没有更多findViewById或Butterknife.Bind(...).除了一种情况外,我发现这一切都没有问题.
例如,在基类中,BaseActivity通常会有一堆视图出现在所有布局中,例如工具栏.和常见的操作changeToolbarColor(),或setToolbarTitle().
在这个简单的情况下,我不能使用Kotlin Android Extensions,因为它是一个基类,视图本身将出现在多个布局上,并且不能导入属性.在这种情况下,我只是简单地使用by lazy {find<>(...).
有没有办法用内置的Android扩展插件完成这个?
在iOS中,是否可以为UI对象分配字符串ID,然后通过该ID在代码中检索它们?
我正在寻找类似Android的东西 findViewById(id)
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文件说"按钮为空"为什么???
搜索和工作了很长一段时间 - 没有运气.(一定很简单?谢谢你的帮助.)
试图获取/设置一个充满EditTexts文本的屏幕,但不是采用通常的,更加硬编码的方式:
... findViewById (R.id.SomeTextWidgetId) ;
Run Code Online (Sandbox Code Playgroud)
相反,我试图通过一个包含(String)name_of_widget的变量来找出一种可重用的方法.
在伪代码中:
findViewById(R.id.>> StringVarHere <<); // 怎么做 ?
我也尝试了这个findViewById方法,但它没有用(!?)
//// given:
static final String FIELD_TV_FEE = "TextViewFee" ;
static final String FIELD_TV_FOO = "TextViewFoo" ;
static final String FIELD_TV_FUM = "TextViewFum" ;
//// and some arbitrary number more of similar fields
static final String [] ALL_FIELDS = {
FIELD_TV_FEE ,
FIELD_TV_FOO ,
FIELD_TV_FUM // ...
} ;
//// ...
//// this part works
int ResourceID;
String stringVarHere = FIELD_TV_FEE;
//// …Run Code Online (Sandbox Code Playgroud) 昨天我遇到了一个问题 - findViewById()为我的工具栏返回NULL.我通过内部人员四处寻找,但似乎我无法找到解决我的"大"问题的方法:D
这是styles.xml
<resources>
<style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar">
<!-- TODO: Create AppTheme -->
<item name="android:windowActionBar">false</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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 = ".Main" >
<LinearLayout
android:orientation="vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width = "fill_parent"
android:layout_height = "@dimen/toolbar_height"
android:background = "@mipmap/bg_toolbar" >
<ImageView
android:id = "@+id/toolbar_drawer_button"
android:clickable="true"
android:layout_width = "wrap_content"
android:layout_height = "@dimen/toolbar_height"
android:src = "@mipmap/ic_drawer" …Run Code Online (Sandbox Code Playgroud) 我使用 Kotlin Android Extensions 插件从 XML 访问视图。如果我理解正确的话,为视图生成的合成属性可以为空,更准确地说 - 它们的类型是View!,这意味着它们可以在 Kotlin 中被视为非空,但实际上它们仍然可以为空,因为在幕后findViewById是调用,它来自 Java,所以 Kotlin 不能确保空安全。这没关系,因为我们可以使用?.运算符。
当您需要在 lambda 中使用这些时,问题就出现了,因为空值被捕获并且您永远没有机会处理非空值。
看这两个片段。在这些被执行的那一刻,view1非空,但view2为空:
//this one crashes
view1.setOnClickListener {
view2.doStuff()
}
//this one works
view1.setOnClickListener {
findViewById<View>(R.id.view2).doStuff()
}
Run Code Online (Sandbox Code Playgroud)
在第一种情况下发生的情况是,当执行该行时,view2为空,而 lambda 将其捕获为空。因此,即使view2lambda 运行(view1被点击)时不再为空,应用程序也会崩溃。
在第二种情况下,lambda 不捕获任何内容,因此即使view2最初为空,每次 lambda 运行(view1单击)时都会再次检索它。
所以问题是:如何在 lambdas 中使用合成属性,而不捕获初始值?
谢谢!
android android-view findviewbyid kotlin kotlin-android-extensions