为充满按钮的内页布局添加触摸监听器
我在线性布局中添加了一个触摸事件,以响应滑动手势,并且效果很好.但是,当我在布局中添加按钮时,父衬里的布局将被忽略.我应该如何防止这种情况发生? LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2); ln2.setOnTouchListener(swipe); 我如何使用onInterceptTouch? 解决方案 您应该创建自己的布局并覆盖布局的Intercepttouchevent(MotionEvent EV)方法. 例如,我创建了自己的布局,该布局扩展了RelatiVelayout @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return true; // With this i tell my layout to consume all the touch
4 2023-10-24
编程技术问答社区
从一个片段加载timePickerDialog
我正在尝试使用自定义时间选择器,然后从片段中运行选择器.这是时间选择器对话框(timepickerfragment.java)的代码: package com.ravit.android.aroundtheclock; import android.app.Dialog; import android.app.DialogFragment; import android.app.Fragment; import android.app.TimePickerDialog; import android.os.Bundle; import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TimePicker; import java.uti
4 2023-10-21
编程技术问答社区
从调用活动中在DialogFragment中设置TextView的文本内容
构建一个Android应用程序并遇到麻烦.我感谢任何帮助! 我创建了一个从Activity A中调用的dialogfragment(councel_create_error)的类. public void setError(String message) { TextView error = (TextView)getActivity().findViewById(R.id.message); error.setText(message); } i然后在活动a中使用此方法 Account_Create_Error error = new Account_Create_Error(); error.show(getFragmentManager(), "error"); error.setError(json.getString("response")); 我似乎从FindViewById获得了nullpointer例外.
10 2023-10-19
编程技术问答社区
如何获得一个动态创建的文本视图的id?
我正在尝试动态添加和删除我正在制作的Android应用程序的TextView,但是我正在遇到困难并获取TextView的ID.对于最后两行代码(et.settext and ll.removeview),我似乎会得到无效的指针异常.有人对我如何动态设置和获取文本视图的ID有任何建议吗?显然,.setID似乎不起作用,或者更有可能,我做错了. //surrounding non-relevant code removed EditText et = (EditText) view.findViewById(R.id.edittext_tags); et.setText(""); TextView nTv = new TextView(view.getContext()); LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lparams.s
6 2023-10-19
编程技术问答社区
在不同的类中,静态方法中的findViewById
内部的Android片段内,我对我的应用程序是否在平板电脑上运行(在平板电脑上我有3次视图打开,观看2和3都在手机上看到,只有第一个,只有第一个,就只能看到3个视图,只有第一个,请看一下我的应用程序,只有以下简单的检查,只有第一个看到一个) boolean mDualPane; View detailsFrame = getActivity().findViewById(R.id.content1); mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; 这在片段本身中正常工作,但是我需要在许多片段中进行完全相同的检查,因此我想将其放在我用于多个类中经常使用的方法的"杂项"类中.现在我班上的同一鳕鱼看起来像这样: public class MiscMethods{ public static boolean landscapeChecker(){
0 2023-09-29
编程技术问答社区
静态方法中的findViewById
我有这种静态方法: 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");
4 2023-09-29
编程技术问答社区
Android:如何在服务类中使用mediaController?
我有一个通过MediaPlayer作为服务播放广播的应用程序.是否可以在服务类中使用MediaController? 问题是我可以使用FindViewById. 我试图在Onpeared Methiod中获得线度延迟... public class RadioPlayer extends Service implements OnPreparedListener, MediaController.MediaPlayerControl { @Override public void onCreate() { super.onCreate(); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnPreparedListener(this); mediaController = new M
18 2023-08-29
编程技术问答社区
安卓系统中屏幕旋转后,ViewPager findViewById为空
我正在使用viewPager显示一些表格.我的问题是,当我旋转设备的屏幕并再次创建viewPager时,当他到达findViewById时,它看不到viewPager,它总是null.我不知道旋转改变时如何处理. 我尝试了onConfigurationChange,但它不起作用,我试图保存Pager的Id,然后使用保存的id进行检索,但也没有.所以我现在被封锁了. pager = (CustomViewPager) getActivity().findViewById(R.id.pager); pager.setPagingEnabled(false); 在OnViewCreated 中 解决方案 在fragment 的onActivityCreate中执行此操作 pager = (CustomViewPager) getActivity().findViewById(R.id.pager); pager.setPagingEnabled(false);
Android fragment-findViewById返回null
我的问题似乎在互联网上很常见:我创建了一个仅用片段的活动.这是生成的代码: @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);
20 2023-06-27
编程技术问答社区
依靠视图查找的效率
大概大多数Android开发人员都知道findViewById不是便宜的操作.我们大多数人都知道的另一件事是,您可以使用视图层次结构的最小子树来提高性能,以通过其ID查看视图,例如: 在这种情况下,您可能想在LinearLayout中搜索id == @id/textview 但是,当层次结构不是级联,而是在各个层面上分支,您想在
6 2023-05-23
编程技术问答社区
在ViewPager中对EditText的引用产生空指针异常
我要做的事情: 有3个不同片段的用户输入数据,然后在第四片段中,用户将在提交数据库之前查看所有数据输入到前3个片段. 我遇到的问题: 我尝试设置或从EditText的每种方法创建NullPointerException 我的问题: 对于我要做的事情,从一个片段中呼吁递观的文本值的正确方法是什么,并在更改页面时以另一个片段中的递观设置该文本? 注意: 我已经简化了代码,以仅反映每个片段上的一个递观,以易于复制和易于解剖代码. 代码: import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerA
安卓系统中通过依次查找视图出现空指针异常
嘿, 今天,我开始开发自己的小警报器应用程序.但是现在我有一个问题,我正在搜索三个小时,无法解决. 当我尝试为每行的自定义设计实现listView时,当我尝试从布局中的文本视图设置文本时,我会得到一个NullPoInterException.此外的原因是,findViewById-call返回null,而不是我想要的文本视图. 在我的搜索中,我找到了这两个帖子,但不幸的是它们没有很好的帮助... post1-stackoverflow post2-stackoverflow 我使用的基本结构与上面的两个帖子相同,但是我找不到错误... 这是我的应用程序中的一些代码: listitem_row.xml:
8 2023-04-30
编程技术问答社区
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); } 调试时,v包含一个Menuview,其ID等于R.id.listViewMenu + 1.当然,这会在菜单上抛出NPE.setColors
8 2023-04-30
编程技术问答社区
在AlertDialog.Builder中使用findViewById()时出现NullPointerException。
这是我的代码 @Override public void onClick(View v) { final AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext()); adb.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom, null)); adb.setPositiveButton("Add",new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { DatePicker datePicker = (DatePicker)findViewById(R.id.date
16 2023-04-30
编程技术问答社区
对于对话框中的视图,findById()返回null。
问题是,无论我在何处或如何要求该布局的组件,它们总是返回null. setView(inflater.inflate(R.layout.search_layout, null)) 这很好.它显示Dialog内的布局,但是,孩子们总是以findViewById(R.id.some_search_layout_children)>. 为null返回 我已经尝试多次清洁项目,试图为我的Dialog实现另一个类,称为findViewById()作为我的主Activity的成员,initSearch()方法和内部的匿名实现OnClickListener的OnClickListener,但所有结果都具有相同的结果.我还尝试将孩子分解为独立View,并以编程方式打电话给他们: TextView text = (TextView) findResourceById(R.id.new_independant_textview); ,但同样是相同的结果. 这是相关代码:
空指针 异常-findViewById()。
任何人都可以帮助我找出该程序可能存在什么问题. 在onCreate()方法中,findViewById()返回所有ID的null,这会导致零指针异常.我无法弄清楚为什么findViewById()找不到视图.有什么建议吗? 这是主要代码: public class MainActivity extends Activity { ViewPager pager; MyPagerAdapter adapter; LinearLayout layout1, layout2, layout3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout1 = (L
96 2023-04-30
编程技术问答社区
安卓:即使是在setContentView之后,findViewById也会返回Null。
这是我的登录代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } loginButton = (Button)findViewById(R.id.loginButton); loginButton.setOnClickListener(new View.OnClickList
12 2023-04-30
编程技术问答社区
无法解决AppCompatActivity中的findViewByID问题
我正在关注本教程: android使用标签的材料设计 infos: Android API级别21 依赖项被添加到Graddle文件中 编译'com.android.support:appcompat-v7:23.0.1'和 编译'com.android.support:design:23.0.1' } 无法解决方法" FindViewById".我试图按照此答案无法解决片段中的findviewbyid(int)' AppCompatactivity使用onCreateBundle而不是oncreateview,那么我无法获得视图对象... 此代码有什么问题?我缺少一些导入? package info.androidhive.materialtabs.activity; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.
64 2023-03-19
编程技术问答社区