findviewbyid

How to get the id of a dynamically created textview?

大兔子大兔子 提交于 2019-12-23 18:36:18
问题 I'm trying to dynamically add and remove TextView for an android app I'm making but I'm running into difficulty setting and getting the TextView's id. I seem to be getting null pointer exception's for the last two lines of code (et.setText and ll.removeView). Does anyone have any suggestions on how I can dynamically set and get the id of a textview? Apparently .setId doesn't seem to work, or more likely, I'm doing it wrong. //surrounding non-relevant code removed EditText et = (EditText) view

View.findViewById() returning null

邮差的信 提交于 2019-12-23 12:17:30
问题 I have this code: 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); } When debugging, v contains a MenuView whose id equals R.id.listViewMenu + 1 . Of course this will throw a NPE on menu.setColors. Why does R.id.listViewMenu contain an

findViewById inside a Static Method

走远了吗. 提交于 2019-12-23 08:05:10
问题 I have this static method: 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,

The method findViewById(int) is undefined for the type Get Data Fragment for Accelerometer Sensor

前提是你 提交于 2019-12-22 10:50:38
问题 I'm doing my project for getting data by acceleometer sensor in android platform. I'm having error codes in the 3 last line codes. Exactly the error is the findViewById is not for get data fragment. so, how I can use findviewbyId in Fragment for getting data accelerometer sensor?many thanks for the answer :) import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle;

Take screenshot method inside Fragment

蹲街弑〆低调 提交于 2019-12-21 21:21:33
问题 I've been trying to get this code working inside a fragment. This code runs fine inside an Activity, but I can't make it to work inside the fragment. When I click a button, it calls the takeScreenshot method. This is the code for the screenshot: public Bitmap takeScreenshot() { View rootView = findViewById(android.R.id.content).getRootView(); rootView.setDrawingCacheEnabled(true); return rootView.getDrawingCache(); } The "findViewById" it's not recognized due to being in a fragment. I've

Kotlin Android View Binding: findViewById vs Butterknife vs Kotlin Android Extension

一笑奈何 提交于 2019-12-17 23:18:03
问题 I'm trying to figure out the best way to do Android View Binding in Kotlin. It seems like there are a few of options out there: findViewById val button: Button by lazy { findViewById<Button>(R.id.button) } Butterknife https://github.com/JakeWharton/butterknife @BindView(R.id.button) lateinit var button: Button Kotlin Android Extensions https://kotlinlang.org/docs/tutorials/android-plugin.html import kotlinx.android.synthetic.main.activity_main.* I'm pretty familiar with findViewById and

adding touch listener for liner layout filled with buttons

断了今生、忘了曾经 提交于 2019-12-17 18:31:52
问题 I added a touch event to a linear layout to respond to swipe gestures, and it works well. However, when i add buttons to the layout, the parent liner layout is ignored. How should I prevent this from happening? LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2); ln2.setOnTouchListener(swipe); How to i use onInterceptTouch ? 回答1: You should create your own layout and override onInterceptTouchEvent(MotionEvent ev) method of your layout. For Example I created my own layout which extends

findViewById() returns null for Views in a Dialog

廉价感情. 提交于 2019-12-17 04:08:27
问题 The problem is, no matter where or how I call for this layout's components, they always return null. setView(inflater.inflate(R.layout.search_layout, null)) This works fine. It displays the layout inside the Dialog , yet, the children are always returned as null by findViewById(R.id.some_search_layout_children) . I've tried cleaning my project multiple times, tried to implement another class for my Dialog , called findViewById() as a member of my main Activity , inside the initSearch() method

ActionBar get current icon, findviewbyid

我们两清 提交于 2019-12-13 16:25:52
问题 I know how to set the icon of the actionbar, but how do I determine what icon is currently there? I don't see an accessor like getIcon , I think it is possible to find the icon view by id, since I've done this with the search widget in the actionbar, but I wouldn't know what id to call. 回答1: According to the source code of ActionBarView.HomeView you should be able to get ImageView of icon by android.R.id.home . So code may look like: ImageView imageView = (ImageView) findViewById(android.R.id

findViewById in non activity class

∥☆過路亽.° 提交于 2019-12-13 07:59:46
问题 I know there are some topics in StackOverFlow asking this But all of them are answering in special cases No one answers this question generally. Can anyone say how to use findViewById in non activity classes? For Example in my case I want to define a webview: WebView view=(WebView)findViewById(R.id.webview); But I can't and I really don't understand why android makes everything complicated.Why you can use everything in main activity but you can't use many of them in non activity class :(