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.
Related
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.
I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.
You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);
Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)
In my app which has tabbed activity, I am sending a simple message to textView and logcat (message indicating which tab I am selecting).
However, textView shows correct string but logcat shows different one.
Steps to reproduce this problem are very simple:
Start a new Android Studio project (My Android Studio version: 1.5.1)
Give any application name (e.g. MyApplication)
Keep only "Phone and Tablet" selected
Select "Tabbed Activity"
Choose Navigation Style "Action Bar Tabs (with View Pager)"
Click finish
This will generate and build project. If we run this application it will show string "Hello World from section: 1" on first tab, "Hello World from section: 2" on second and "Hello World from section: 3" on third tab.
Now, go in MainActivity.java and in method onCreateView (of class PlaceholderFragment) just below this line (where it send string to textView),
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
add this line
Log.i("onCreateView", "From tab number: " + getArguments().getInt(ARG_SECTION_NUMBER));
Expected behaviour:
textView and logcat both should display same message as we tap on tabs.
Actual behaviour:
textView shows correct strings. However in logcat, tab numbers shown are incorrect and hence not in consistent with textView output.
This is indeed weird and height of all is, instead of this when I tried forming string on my own and passing it to textView and LogCat
String strMessage = "Msg from tab number: " + getArguments().getInt(ARG_SECTION_NUMBER);
textView.setText (strMessage);
Log.i("onCreateView", strMessage);
It still has SAME behaviour i.e. shows correct strings in textView for relevant tabs but Logcat shows completely different tab numbers. Same string I am sending but only number changes ???
I found root cause of this on my own. Looks like textView.setText called in onCreateView sets the text value and it never changes afterwards. I wanted to find out which tab currently is active or visible so that I could update its contents. And I was under wrong impression that onCreateView gets called when tab is visible. Finally I found this answer which solved my problem.
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
Im having some trouble with setting textview to invisible/visible.
basicly i want this to happen when an on/off button has been clicked.
what i did is kind of like
textview.setVisibility(TextView.VISIBLE);
textview.setVisibility(TextView.INVISIBLE);
when i try executing this the emultor says that the app has stopped unexcpetedly
Are you building this from XML or programmatically?
I would make it with an XML file then when the Activity runs change the property. Be sure to use setContentView(R.layout.main); before you try to get the TextView with findViewById(...).
Call .setVisibility(View.GONE); on the TextView to hide it.
Call .setVisibility(View.VISIBLE); to on the TextView to show it.
I have an example that does something like this. You can see the code here: https://github.com/ethankhall/Morse-Messenger/blob/master/src/com/kopysoft/MorseMessenger/Translate.java
Without more code or a stack trace, it's hard to say, but it sounds like you haven't initialized the text view. Here's how to do it:
TextView myTextView = (TextView) findViewById(R.id.tv_text);
Where 'tv_text' is the id of the textview as defined in the xml layout file.
Hope that helped!
Read about DDMS and logcat to obtain a stacktrace and to see what the problem is: http://developer.android.com/guide/developing/debugging/debugging-projects.html
This is what you are looking for: