- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 android fragment 中,我进行了以下简单检查,以判断我的应用程序是否在平板电脑上运行(在平板电脑上我打开了 3 个 View ,在手机上看不到 View 2 和 3,只有第一个看到一个)
boolean mDualPane;
View detailsFrame = getActivity().findViewById(R.id.content1);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
这在 fragment 本身中工作正常,但我需要在许多 fragment 中进行完全相同的检查,因此我想将它放在我用于多个类中常用方法的“MiscMethods”类中。现在我类的同一条鳕鱼看起来像这样:
public class MiscMethods{
public static boolean landscapeChecker(){
boolean mDualPane;
View detailsFrame = getActivity().findViewById(R.id.content1);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
return mDualPane;
}
}
getActivity(如果我删除 findViewById)对于 MiscMethods 类型是未定义的。
现在我有一个像这里这样的上下文应用程序类
public static void ErrorToast(int errorCode) {
String errorString = null;
switch (errorCode) {
case 1:
errorString = App.getContext().getString(R.string.error_tooManyFieldsEmpty);
break;
case 2:
errorString = App.getContext().getString(R.string.error_featureComingSoon);
break;
case 3:
errorString = App.getContext().getString(R.string.error_SwitchBreak);
break;
default:
errorString="Wrong Error Code";
break;
}
Toast errormsg = Toast.makeText(App.getContext(), errorString, Toast.LENGTH_SHORT);
errormsg.setGravity(Gravity.CENTER, 0, 0);
errormsg.show();
}
但是这个 App.getContext() 也没有帮助。
我该如何解决这个问题?
最佳答案
看起来您正在寻找一种快速而简单的解决方案,所以这里有两个:
更改方法签名以将 Activity 作为参数:
public static boolean landscapeChecker( Activity Activity )
。
在该方法中,使用传入的activity
代替getActivity()
。从您的各种 Activity 中调用它,例如 boolean mDualPane = landscapeChecker(this)
。
子类 Activity
并将方法放在那里。对于此解决方案,您将创建一个类 MyActivity extends Activity
,然后让您的各种 Activity extend MyActivity
而不是 extend Activity
。在该方法中,使用 this
代替 getActivity()
。
关于android - 不同类中静态方法中的 findViewById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700588/
我是 android 编程的新手,我对 View.findViewById() 和 Activity.findViewById() 很困惑,我们什么时候必须调用这些方法以及在哪个方法中调用类 find
/** * A placeholder fragment containing a simple view. */ public static class Placehol
我尝试在 java 中设置来自 TextView 的文本:首先我创建了一个这样的对象: //Property info TextView propertyInfo = (TextView) info.
[更新] -- 只是想向所有人确认,我同意使用硬编码号码总是一个坏主意。我的好奇心是在使用 Maps API 时枚举的 1、2 常量值是什么。如果我找到像 R.id.my_current_locati
在我的 fragment 的 onCreateView 方法中,我曾经将膨胀的布局存储在一个变量中,然后将后者传递给我的其他函数,这些函数在这个膨胀的 上调用了 findViewById >查看。 现
我正在尝试实现自定义数组适配器, 问题是我的代码在使用时崩溃 ImageView imageView=(ImageView)findViewById(R.id.imageView); 而不是 Ima
我想在多个 Activity 中使用 View 。就我而言,它是一个 FloatingActionButton。当我通过 findViewById 搜索 View 时,程序抛出 NullPointer
findViewById 给出一个错误,我认为这是因为它返回 null。我的应用程序被迫关闭。 SensorManager sensorManager=null; TextView x= null;
我刚刚创建了简单的应用程序并添加了按钮。 MyActivity.java package com.example.myapp; import android.app.Activity; import
我的应用程序在布局中有许多元素(远超过 100 个),并且在 MainActivity 中声明所有元素以便访问它们,这使得我的 MainActivity 类难以阅读和调试。因此,我创建了一个新类来处理
我刚刚开始android开发,我被困在这个: static Handler h = new Handler() { @Override public void handleMessag
我是一名初学者,正在磨练我的应用程序开发技能。我目前正在做一个示例,其中列表由字符串数组填充。在最后的步骤中进行绑定(bind),这是找不到方法“findviewbyid”的地方,并且在构建后显示为红
这是我函数的一部分,android studio 无法识别 findViewById 函数。谁能帮我找出问题所在吗?\ public void onClick(View v) {
我在 LinearLayout 内的相对布局内访问 TextView 时遇到问题 假设我在文件 row.xml 中有这些 View
很抱歉标题含糊不清, 下面是麻烦。我想使用 x 变量来迭代该对象,该对象在名称中使用 1 -9 值 (R.id.imageButtonx) for (int x = 1; x <10; x++)
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我做错了什么,我检查了几次ID,在XML中一切正常,但是当我想构建发布应用程序时,会出现此错误,当我构建调试应用程序时,我不会出现此错误。 error: cannot find symbol
我是 Android 编程新手,所以我有一个简单的问题!我在 activity_main.xml 和 main_activity.java 中创建了一个 button 我想通过调用 findbyvie
我对 Android 开发非常陌生,所以如果我无法理解这里到底发生了什么,我很抱歉。 我创建了一个形状,如下所示。它包含在可绘制文件夹内的 shape.xml 中: 现在在我的 Acti
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我是一名优秀的程序员,十分优秀!