I have created multiple XML files, but today I reopened my project and I am unable to set the content view to my newly created layout. When I Type "R.layout" and look at the XML files it recommends; I see a list of XML files, but not the one that i want. I have tried cleaning my project and rebuilding everything, but it didn't work. I have also made sure that i'm not importing "Android.R". I am not sure why this is happening. Thanks for your help.
XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Tabs Class java
package com.thenewboston.cfil360;
import android.app.Activity;
import android.os.Bundle;
public class Tabs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
}
}
Related
I'm writing a simple Android App using AIDE (Android IDE). I gave one of my layout elements an ID, but when I try to access the element using findViewById(), I get an error tht says: "Unknown member 'id' of 'com.mycompany.mailscomunes.R'. I haven't seen this error outside of AIDE.
This is the Java code:
package com.mycompany.mailscomunes;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.provider.ContactsContract;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.one);
}
}
And this is the relevant XML:
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/one"/>
Remove these lines:
import android.app.*;
import android.os.*;
You're literally importing the entire Android framework, which includes layout files, which has ID's in them.
And you're not including your own ID file (called "R.java").
So remove those two lines, and include this one:
import com.mycompany.mailscomunes.R;
The root of an activity xml should be of Layout type.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Text"/>
</LinearLayout>
Goto your project and delete the build folder and do a rebuild. I had the same problem and is fixed
I did everything just as stated in this tutorial:
google android basic tutorial
and despite everything being done just as described, the code refuses to compile with 3 errors. Looks like the guys writing the turorial forgot to mention what are those things and where/how do I define them.
The errors I get:
Error:(24, 68) error: cannot find symbol variable container
Error:(36, 23) error: cannot find symbol variable action_settings
Error:(46, 54) error: cannot find symbol variable fragment_display_message
Neither of the 3 fields are defined anywhere (Perhaps one of the libraries is wrong?)
The file in question is:
package com.example.asteroth.first;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.R;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
// setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
return rootView;
}
}
}
I am using Android Studio I just downloaded and no question from search or Similar Questions points at the problem like this one, hence I suspect authors of tutorial forgot to mention something minor. I've seen suggestion to place the "container" as a new ID in one of the XML files, but to no avail.
EDIT:
'cannot find symbol ActionBarActivity' following Android Development Tutorial?
This post suggest a solution, however it changes ActionBarActivity to just Activity which is very different from what the tutorial uses and I don't know how serious repercussions would it cause
EDIT2:
Problems found and removed:
import android.R //causes action_settings error
container missing //had to add it in the xml file as an id
xml file named wrong //If I got that correctly, I'm still waiting for someone experienced to clarify, but seems like the tutorial used different name for the xml file then the one that the java code references
Remaining problem is similar to this one
Cannot resolve method placeholderfragment error
however, I both extend Fragment and include android.app.Fragment as can be seen in the included file.
I tried the same tutorial and here is how I fixed my errors:
R.id.container cannot be resolved error
I had to import android.support.v4.app.Fragment to fix this problem and add android:id = "#+id/container" to the RelativeLayout section in my activity_display_message.xml file.
fragment_display_message cannot be resolved error
Change R.layout.fragment_display_message to R.layout.activity_display_message instead. There is no need for creating a new xml file for fragment_display_message.
This should fix these two errors.
But you would probably be better off if you comment out the if(savedInstanceState............ statement as otherwise your program would crash once you try to run it if it doesn't give you any errors.
Your onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
/*if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
I'm doing the tutorial for the first time now on Feb. 23, 2015 and ran into this compilation error though I feel like I've closely followed the steps. I changed fragment_display_message to activity_display_message which is an XML file they have us create in the tutorial. This seems to solve the error, and allow the app to run.
// A placeholder fragment containing a simple view.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
Add this line to take care of your first error: android:id = "#+id/container"
You get that error because container isn't in the XML.
Add <string name="action_settings">Action Settings</string>so that the "Action settings" which I'm assuming doesn't exist in your XML code since you have that error.
Create your own XML file with this exact name fragment_display_message.xmlto handle that error and check what code you might need to insert into it in your google tutorial. Often times with Eclipse, these files are not included for reasons outside my knowledge. So you have to create them or insert them yourself. (Make sure you have the latest version of the SDK by the way.
EDIT: Be sure to have the correct imports matching with your "tutorial". I took a gander at it and see you missing two imports. One of which another user answered.
It's a copy paste error.
If you paste code with "R." in it, the development environment always imports the android.R:
import android.R;
If you use R.id.... it is always looking up the android.R and not your own generated R class.
Delete the import and it should be fine. This general works for me.
After that you have to check if you already defined the id's and layout.
You can check Layouts by looking on the package explorer under res->layout. In your example there has to be an fragment_display_message.xml in it.
For id's you have to look up all of your layouts and check if there are the specific views like the container.
I got a similar error on the Building a Simple User Interface step:
Error:(18, 54) error: cannot find symbol variable toolbar
I've narrowed the cause down to res/layout/activity_my.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
The original version that did compile (but no button or text box) is:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MyActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_my" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
fragment_display_message
Make sure you have a file named fragment_display_message.xml in your res/layout folder.
action_settings
Make sure you have that item in your menu.xml file in res/menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=
".MenuExampleActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
app:showAsAction="never"
android:title="My menu option"/>
</menu>
container
Make sure you have a layout (ex. RelativeLayout) with the id set to "container" in your activity_main.xml file in res/layout, given that it's the reference for the code to insert the fragment there.
When I want to create a XML file in Android SDK installed ECLIPSE for the purpose of SharedPreferences Activity layout... I do not see that dialog box to appear where I can choose from.
Following are my files.
MainActivity.java
package com.preferences_activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
Preferences.java
package com.preferences_activity;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="#string/button1" />
</RelativeLayout>
preferences_activity.xml
<?xml version="1.0" encoding="UTF-8"?>
In the preferences_activity.xml, this is what I get when I create a xml file.
But I wanted to choose a type called preferences while creating a xml file. preferences layout is one of the type of resource available in the dialog box. Others are like: Layout, Values, Menu, AppWidget Provider Searchable, Animation.
If I had a option to choose a preferences layout option while creating a xml file I would be getting the following code:
<?xml version="1.0" encoding="UTF-8"?>
<PreferencesScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
My Question is again: Where can I get that dialog box from while creating xml file where I can choose the resource type?
I am sorry if this question sounds funny. But I am struggling with it for few days in a row.
Thank you all.
For future Visitors:
I was struggling because I was using eclipse app instead of the one that was under adt-bundle. If you use the app under the adt-bundle, that has a logo with {} and light green background than you will have an options to choose a layout or any sorts of resources while creating an xml file. Pls, one more thing to note, I was having hard time to find a wizard for creating a xml file ( I could do it manually with right clicking the project and other and choosing xml file).. but I m talking about the wizard on the menu bar, you should be able to see it if you use this app under adt-bundle instead of regular eclipse. My answer may not be that likable, but sharing for future android beginners.
Ex: I have a project with UI contains a button..
Activity:
package android.tuanshaker.myproject.com;
import android.app.Activity;
import android.os.Bundle;
public class MyProjectActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And file main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
I want to display code and xml of my project by textview..How do i can to get them?
Copy the xml, js code files to res/raw. Use Context.getResources().openRawResource() to read them.
In addition, you can use jHighlight to format those source codes (in a web view).
I have been trying everything to get this to work, but to no avail. I tried to use the (AutoResizeTextView) posted here: Auto Scale TextView Text to Fit within Bounds
I created a new class file in my project with the name "AutoResizeTextView" and pasted the above code.
Then i opened the main.xml file and put n the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.mn.rl.AutoResizeTextView android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
in the mainActivity.java file, i have the following code:
package com.mn.rl;
import android.app.Activity;
import android.os.Bundle;
public class rlActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
com.mn.rl.AutoResizeTextView txt = (com.mn.rl.AutoResizeTextView) findViewById(R.id.tv);
txt.setText("Hello");
}
}
I have no errors in the code, and it runs but there is no autoresizing. the text remains the same size. in the xml i have the autoresize textview layout_width and layout_height set to fill parent, yet the font remains small. also tried txt.resize().
What am i doing wrong? Please Help.
there is a slight possibility that that class is only meant for resizing down. not up.