I keep getting an error that says cannot resolve method findViewById . I understand that this is probably because findViewById method works with a specific view or its descendants . I would like to check a radio button in another view whilst I am in another view. How do I then reference that view without having to inflate . This is the line of code that is giving me the "cannot resolve method findViewById" error . This radio button is actually in another view.
RadioButton r1 = (RadioButton) findViewById(R.id.rb_data_all);
r1.checked(true);
You said:-
"cannot resolve method findViewById "
That means it is undefined , and I assume you want the one from Activity Class so
try this.
1) Extend your activity class to android.support.v7.app.AppCompatActivity
2) Override onCreate(Bundle instance) at least and it should work.
Related
I get a massage says "app is keep stopping" when I run the project on my mobile.
the error at on Create(MainActivity.java:16)
at the line 16 set Content View method
help me
The method setContentView() sets the View that your MainActivity should display.
The View is usually declared in layout/activity_main.xml.
Some error seems to appear when trying to call this method in your application.
A common reason why android applications crash there is calling some methods in the overridden method onCreate(Bundle savedInstanceState) in a wrong order.
Check if your code follows this pattern:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView call
// other things
}
I cannot help you further now, please copy and paste the stacktrace (crash information) into your question. When your application just crashed on an emulator, you can see it in the Run-section (usually red text).
I am new to Android development. My app is compiling perfectly fine (no errors, no warnings), but it crashes on launch. I have diagnosed the cause of the crash to be a NullPointerException, and I discovered that if I remove TextView output = (TextView) findViewById(R.id.outputbox); as well as all the lines depending on this declaration, the app launches fine. I did research (both on and off Stack Exchange, and about 5 pages of Google results) and none of the solutions I have found worked, which is why I am asking this question. I have ran setContentView(R.layout.activity_main); before TextView output = (TextView) findViewById(R.id.outputbox); and outputbox is declared in activity_main.xml. This is not a duplicate of the existing questions because the answers to the other ones did not solve my problem.
most probably your declaration and initialization of the variable output is in the class. This is not going to work. The method findViewById will work only after the setContentView method has been invoked. That is why you have to declare your variable in the class, but initialize it in the method onCreate immediately after the setContentView method:
public class MainActivity extends AppCompatActivity {
TextView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.outputbox)
}
}
The method findViewById searches for your component in the xml. For this to work, you have to point out what xml file will this Activity be using. This is done in the setContentView(R.layout.activity_main); command. It is a common mistake, so no worries :)
TextView quantityTextView=(TextView) findViewById(R.id.quantity_text_view);
When creating object for quantityTextView reference we use id from xml that is quantity_text_view but how object is created for it without using new keyword.
I have searched a lot but haven't found the solution. I'm new in Android Programming, if you know the answer please tell how you find the solution for it
On your onCreate method you are doing something like that
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
on the line setContentView(R.layout.main_layout); you are setting the layout and then you just getting reference on elements from that layout
And because of the UI has been set you don't need to create something with the new keyword just to get the reference with the findViewById
If you want to add dynamically a layout element you will need to use the new keyword and then add that element on your layout
You can use android-layout-finder to without using new keyword. https://www.buzzingandroid.com/tools/android-layout-finder/
Declare in class:
private TextView quantityTextView; //this is already a object
In your onCreate() init the View:
TextView quantityTextView=(TextView) findViewById(R.id.quantity_text_view);
Now you can use quantityTextView anywhere without any new creation.
If you want to add Textview dynamically you need to use Text view like this:
TextView tv=new Textview(this);
And if you want to inflate the Textview from the XML file then you need to use TextView like this:
TextView quantityTextView=(TextView) findViewById(R.id.quantity_text_view);
findViewById does nothing. It just looks through view hierarchy and returns reference to a view with requested viewId. View is already created and exists. If you do not call findViewById for some view nothing changes.
Views are inflated by LayoutInflator. When you call setContentView xml layout is parsed and view hierarchy is created.
attributes passed to Button's constructor by LayoutInflater. check LayoutInflator source code.
What is the intent behind this piece of code?
Button sButton = (Button) findViewById(R.id.button_1);
In your XML files, you create IDs for the widgets you put on the screen.
In that code, you are creating a button reference (sButton) to be the button corresponding to the ID of button_1
findViewById searches for a button based on its ID - the ID is located in the R.java file of your project: Project Folder > gen > package > R.java
The R.java file holds references to everything (or basically everything?) in your project. It's an essential part of it.
That is why the parameter of findViewById is R.id.button_1 because you are searching for the ID of button_1 in the R class (it's a static field).
You are then casting that ID of the button_1 to a button with the (Button) in front of the findViewById.
Extra note:
If you look at the R.java class, you have it declared as: public final class R { so that's where the R in R.id.button_1 comes from.
Then you have another inner class like this: public static final class id { so that's where the id comes from in R.id.button_1
Then, in the id inner class, you have this (amoung other things):
public static final int button_1=...; where the ... would be some code to represent the int value. That's where the button_1 comes from in R.id.button_1
So bascially, R.id.button_1 goes to the R class, then the id inner class, then accesses the actual int value of the id name.
it finds a view in the associated layout XML by it's id, and casts it to a Button. Was it what you were asking?
Its used to find a Button which you have created in XML and bring it into the Java code so you can work with it. If you didnt do this you would not be able to give the button anything to do.
I get an error like this in logcat when i try to run my script. I compared it and it is the onClickListener. Any suggestions to fix this problem? Still a beginner.
The problem exists here:
View splashscreen = (View) findViewById(R.layout.splash);
splashscreen.setOnClickListener(this);
You are getting an exception because splashscreen is null, and calling setOnClickListener() on a null pointer is not allowed. The reason that pointer is null is because you need to obtain a reference to the view from your XML using a proper ID. Your splash.xml file located in res/layout is being loaded as the content view for the Activity, but you should have a proper R.id value associated with that particular view.
In splash.xml, the View you declare for this purpose should have an android:id="#+id/something" attribute in it's XML declaration (I picked "something" out of the air, this identifier can be whatever you want). You would then call:
//Hint: You don't have to cast the result if the pointer is a plain vanilla View
View splashscreen = findViewById(R.id.something);
splashscreen.setOnClickListener(this);
Then you will get a valid reference to the view and your set method will not fail.
HTH
As Devunwired said, I think maybe you wrong about layout and id.
See http://developer.android.com/reference/android/view/View.html
Yes, Devunwired is right. You must have to give the Resource id to the Perticular view. Instead of that you are going to give the reference of the Layout file name as "splash.xml".
Also try to make the Id name different from the layout file name. Its not and error but sometimes happend that make possible to arrise problem to understand and to give the resource id to different reference.
Thanks.
Instead of using Thread, you can try Handler:
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
//TODO
}
});
By the way, posting more exception stack traces would be more helpful.