I've been trying to make a simple randomizer app that takes an upper and lower bound from user input and displays a random number in between. However, a bunch of my objects don't capture the ID from the XML at all.
Here's the beginning of my OnCreate method for this (second) activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mode2);
Button altRollButton = findViewById(R.id.altRollButton);
TextView altResultHeader = findViewById(R.id.altResultHeader);
TextView altResult = findViewById(R.id.altRollResult);
EditText fromValue = findViewById(R.id.fromValue);
EditText toValue = findViewById(R.id.toValue);
FloatingActionButton switchModeButton = findViewById(R.id.switchModeButton);
And here are the XML ID entries, respectively:
android:id="#+id/altRollButton"
android:id="#+id/altResultHeader"
android:id="#+id/altRollResult"
android:id="#+id/fromValue"
android:id="#+id/toValue"
android:id="#+id/switchModeButton"
If I debug the code and put a breakpoint after all instantiations, here's the log -
Seems like half of my objects properly capture the ID, and the other half don't, and I've been trying to solve this for the past 2 days with no success. Any ideas are very much welcome!
Managed to find the source of the issue. Turns out in the AndroidManifest.xml file I had another default activity with no contents in it and when I commented that out the IDs are now properly captured and everything works as expected.
Related
I am a newbie of Android Studio.
I have a problem with display the logs in my app.
For example:
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cycle);
TextView textView = (TextView) findViewById(R.id.cykleoncreate);
Log.d("[ " + timeStamp + "]", "[onCreate]");
I only want to display this log in my app. How can I do it?
you can display the logs by Toast or set in textView
Toast.makeText(getApplicationContext(),timeStamp,Toast.LENGTH_SHORT).show();
or
textView.setText(timeStamp);
What you have done is write but you can get confused as in where's the log, Log has tag and message, Tags are usually Activity or fragment name so that its easier for one to locate from where did a particular error or message came from, and you can include anything in the message part of it.
we have different types of logs this can be found here
You will find a logcat button on bottom side left side of android studio from there you can select depending on which type you want to see? in your case it's debug
Ok, it works well
textView.setText(timeStamp)
But I don't know why it doesn't work, when I want to build my string:
textView.setText(timeStamp + "my string")
Actually Log is not displaying in app , it will display in android studio . For displaying in app , we need to use Toast . your code is working fine you can check it in your android studio , as its shown in image We can display like this Log.d("onCreate", timeStamp); and search using TAG as onCreate.
When I place "setContentView" above the "NumbersClickListners" line the app works as expected.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
}
But as soon as the "setContentView" is placed below the three lines starting with "NumbersClickListners" the app crashes. The code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
setContentView(R.layout.activity_main);
}
I'm pretty much unsure of the reason for this behavior. Can anybody help me with that please?
Let's look at the life of layout.
First of all, you have to create it by declaring XML file, where you do your design in a user-friendly way and name the elements according to your needs.
Now your layout is just a file, that Android doesn't really care about for performance reasons.
Next thing you wanna do is use your layout. To do that your file needs to be converted to internal structure of objects known as ViewGroup and Views.
This process is called inflating. That's the point where the system can find the views for you by calling findViewById().
So in the second snippet you ask activity to find you a button, which is not inflated. That leads to throwing exception.
Generally speaking, first thing you want to do in the onCreate is to call setContentView().
I'm trying to display numbers under a root sign inside a TextBox or a TextView in java. I can see the root sign, but I can't see the overline.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_mathview);
sqrtDisplay = (TextView) findViewById(R.id.sqrtDisplay);
sqrtDisplay.setText(Html.fromHtml("<html><strike><o>&radic X + 1</o></strike></html>"));
}
What am I doing wrong here? This should be so easy but it's not.
The square root symbol can be displayed in android using the unicode : "\u221a"
but this won't give you the overline.
There's no such support for symbols to write equations in Android but you can use some of the libraries that are available to achieve that, one of the good options is to use jqMath with webview.
I'm just beginning to learn to develop android apps, using eclipse.
I created multiple .xml files and put a textView in each one. However, they are all called textView1. So in my main activity, if I want to declare a textView, like
TextView hello = (TextView)findViewById(R.id.textView1);
how will the compiler know which xml file to look at? How should I specify?
In the method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
here you are setting one xml to your activity.
and when you are declaring
TextView hello = (TextView)findViewById(R.id.textView1);
so textview1 of main.xml will called.
When you specify a file in the onCreate using setContentView, it knows which xml file you are talking about
You dont need to worry about that. Android system is smart enough to find the exact id.Android gets the view id from the layout you specify in setContentView() method.
So feel free to use same id in other xml layout file.
Note:--
In same xml file, id of any View should be unique.
I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference