I am new to Android development. I have created many screens and all are working quite nicely except one.
I am not sure why screen is not coming up as per XML. Below is my code :
<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="vertical"
android:padding="10dip"
android:id="#+id/linearMain">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#2258A2"
android:layout_marginBottom="10dp"
/>
<TextView
android:id="#+id/assignmentTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/lesson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Lesson : Lecture 5"
/>
<TextView
android:id="#+id/dueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Due Date : Nov 20, 2014"
/>
<TextView
android:id="#+id/marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Marks : 20"
/>
<Button android:id="#+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
/>
</LinearLayout>
And here is main class :
public class AssignmentDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcement_detail);
Intent intent = getIntent();
//String course = intent.getStringExtra(Assignments.COURSE);
//TextView textview = (TextView) findViewById(R.id.courseTitle);
//textview.setText(course);
String announcementTitle = intent.getStringExtra(Announcement.ANNOUCEMENT_TITTLE);
TextView announcementTitleTextView = (TextView) findViewById(R.id.annoucementTitle);
announcementTitleTextView.setText(announcementTitle);
}
And here is output :
You can see there are 4 textView and one button in my xml but i can see only onr textView in output, why it is so and how to fix it.
Thanks..
Anjum
Make sure following stuff is correct in ur activity :
Make some changes in layout file and see it display that changed stuff
Correct xml file layout is specified at setContentView.
It seems that you are using some other layout xml..try removing textview and run it..if it still shows that Assignment #1 then ur xml itslef is wrong
I think you are not inflating the right XML layout file.
You can see that the TextView that you modify at the end of your code is not present in the XLM file. Therefore the layout that you inflate cannot be the one you are showing us or you would have an error at run time.
Related
The answer may be staring right at me but I just can't see it!
Am getting a null pointer exception when setting the text on a Textview with a string from a ArrayList of Strings. Have initialised everything and checked the ArrayLiist has data, everything seems to check out. But when the textview is been declared in onCreate() I can see in the debugger the value remains null.
OnCreate():
private TextView title;
//onCreate()
setContentView(R.layout.gallery);
imageNames = new ArrayList<String>();
title = (TextView) this.findViewById(R.id.tv_imageDescriptionGallery);
setImageAndTitleArrays();
//check size of arrays and build correctly
Log.d("GALLERY VIEW:" , "IMaegNaem array Size: "+imageNames.size() + " Image Array Size: "+ bitmapArray.size());
Log.d("GALLERYVIEW: ", "First Image Name of Array: "+ imageNames.get(0));
//set default image title and description, we will update every tome a image clicked
//exception throne here as title=null on debugger
title.setText(imageNames.get(0));
The textview :
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>....
Gallery XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Astro Gallery"
android:textColor="#55bbfa"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Gallery
android:id="#+id/gallery1"
android:layout_marginTop="100dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/splitter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/sample_note"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/splitter" />
<ImageView
android:id="#+id/imageView1"
android:layout_marginTop="100dp"
android:layout_width="420dp"
android:layout_gravity="center_horizontal"
android:layout_height="350dp"
android:src="#drawable/camera1" />
<TextView
android:id="#+id/tv_imageDescriptionGallery"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image Name"
android:textColor="#55bbfa"
/>
<Button
android:id="#+id/send"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Set Wallpaper"
android:background="#drawable/button_bg"
android:textColor="#fff" />
<ImageView
android:id="#+id/shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/shadow" />
</LinearLayout>
</ScrollView>
Only 2 hypothesis :
Maybe you tried to import R.java and you imported the wrong one ?
Or try to remove "this" in front of findViewById?
The 2 only ideas that come to mind, hope it can help.
Although I checked all the imports, removed all the ref to 'this, rename the class and all refs to it, cleaned and build, could not resolve the NPE.
I deleted the class and xml from the project, then made a new class and xml file with same code and worked fine.
I had a class with exact same name in a separate project, not sure if this has anything to do with it.
One of my textViews is not being adjusted when I call the function:
public void wordList() {
setContentView(R.layout.activity_main);
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
Here's the xml:
If I add android:text="Text" to the xml it works.
<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"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:background ="#268496" >
<LinearLayout android:id="#+id/linear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/prefixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="12pt"
android:maxLength="1"
android:typeface="sans" />
</LinearLayout>
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
</RelativeLayout>
If you call setContentView() after running it here then it will overwrite this call and set the content to its default (what's in the xml). If you call setContentView() say in onCreate() then you don't need to call it again as long as the TextView is inside that xml.
So, call setContentView() in onCreate() then call your function
wordList();
then
public void wordList() {
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
You have white color of textview's text. So if the background color is white, then you just don't see it.
You're not doing anything "wrong" in your posted example. The kinds of things that may be in play here are:
a) is your TextView within the visible area (hard code some text to see if it shows up)
b) are you sure you're calling the method that sets the text?
c) do you have some other overlapping ID somewhere else that is confusing this process?
It has to be something like that.
I have below layout i.e 1 spinner and a linear layout containing some edit box's and spinner's.this layout maps/represent to an object (Lets call it X).
The main spinner will be having n number of entries and each entry maps to the layout object (X).So, In all i will need to have n number of layouts
I want to allow user to fill only 1 object at a time ,so, I would keep only 1 layout visible.In order to solve this , One way would be having n number of layouts in .xml and playing with the visibility in onitemselected of the listener.Is there any other better/optiomized way of solving this.
How can i make this dynamic i.e if i dont know the value of n initially ?
`<Spinner
android:id="#+id/linesspinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/linView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/linename1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linecffiltext" />
<Spinner
android:id="#+id/trospinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Tro_arrays"
android:prompt="#string/linetrotext" />
<EditText
android:id="#+id/line1troval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
<Spinner
android:id="#+id/cfspinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/cf_arrays"
android:prompt="#string/linecffiltext" />
<EditText
android:id="#+id/line1cfval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
try this, put that LinearLayout in another xml and call that xml as many times you want inside loop.
sample xml, name attrib_row.xml
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lable"
android:layout_width="150dip"
android:layout_column="0"
android:padding="3dip"
android:textColor="#000033" />
</Linearlayout>
code to call that in loop
Linearlayout row11 = (Linearlayout) LayoutInflater.from(this).inflate(
R.layout.attrib_row, null);
((TextView) row11.findViewById(R.id.lable)).setText("example");
I have an activity that displays a list of restaurant dishes. Each view of the list has a number picker associated with it. The application runs fine, however when I start the activity to display the list it crashes...
This is my class file.
NumberPicker np;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starters);
createTestData();
adapter = new MenuItemArrayAdapter(this, starters);
this.setListAdapter(adapter);
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
}
This is my xml layout file for the StartersActivity class.
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="670dp">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
layout="#layout/pricebar" />
This is my xml layout file for the row in the list.
<?xml version="1.0" encoding="utf-8"?>
<NumberPicker
android:id="#+id/numpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/dishpic"
android:layout_width="140dp"
android:layout_height="150dp"
android:layout_alignBottom="#+id/numpick"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/reviewBtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/item_price"
android:text="ITEM NAME"
android:textSize="20sp" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/reviewBtn"
android:layout_below="#+id/item_name"
android:layout_marginTop="17dp"
android:layout_toRightOf="#+id/dishpic"
android:text="ITEM PRICE"
android:textSize="20sp" />
<Button
android:id="#+id/reviewBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dishpic"
android:layout_toLeftOf="#+id/numpick"
android:layout_toRightOf="#+id/dishpic"
android:text="#string/reviewBtn" />
It seems to be the number picker that is causing the problem because when I take out the following code it works.
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
Any ideas as to what might be the problem? The error is a null pointer exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.StartersActivity}: java.lang.NullPointerException
I have a feeling it is something to do with trying to access the number picker before the list is loaded and it's not seeing it.
To fetch the NumberPicker in the onCreate method it would have to be defined in the main layout. Since it is not you get the NPE.
It depends what you do with the NumberPicker where the best place is to fetch it. It must be specific to the list item view in question.
Each view of the list has a number picker associated with it
The above statement makes me believe that the NumberPickers controls should be referenced and initialized in the MenuItemArrayAdapter in the getView() method, versus in onCreate() how it is now:
np = (NumberPicker)findViewById(R.id.numpick);
np.setMinValue(0);
np.setMaxValue(99);
So I've been trying to figure out layouts and using the layout inflater, but I'm running into some issues. I have two relative xml layout files, the one xml file has two textViews, an editText field, and a spinner object:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/goalNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:text="#string/goalName" />
<EditText
android:id="#+id/goal_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/goalNameView"
android:hint="#string/editText"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/goalTasksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/goal_tasks"
android:layout_alignBottom="#+id/goal_tasks"
android:layout_alignLeft="#+id/goalNameView"
android:text="#string/goalTasks" />
<Spinner
android:id="#+id/goal_tasks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/goalTasksView"
android:layout_marginTop="51dp" />
</RelativeLayout>
the other xml file has one textView and one editText field:
<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:id="#+id/tView"
>
<EditText
android:id="#+id/taskNameField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/taskNameView"
android:ems="10"
android:hint="#string/editText" />
<TextView
android:id="#+id/taskNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/taskNameField"
android:layout_alignBottom="#+id/taskNameField"
android:layout_alignParentLeft="true"
android:text="#string/taskName"
/>
</RelativeLayout>
I'm trying to patch the two layouts together with a third xml file (main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/theMain"
>
<include android:id="#id/mainLayout" layout="#layout/activity_goal_keeper" />
</LinearLayout>
The way I'm looking to have the layout work is to display the first layout (#id/firstLayout) and then the second layout (#id/tView) directly beneath it. I've read that I need to implement the layouts on a LinearLayout to achieve this which is why I have the third layout (#id/theMain).
As you can see I have an include statement for #id/firstLayout, but I don't have an include statement for #id/tView because I'm trying to inflate multiple versions of #id/tView via my main activity:
public class GoalKeeperActivity extends Activity implements AdapterView.OnItemSelectedListener {
public Integer[] items= {1,2,3,4,5,6,7,8};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.activity_goal_keeper);
setContentView(R.layout.main);
Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks);
numOfTasks.setOnItemSelectedListener(this);
ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numOfTasks.setAdapter(aa);
ViewGroup item = (ViewGroup) findViewById(R.id.theMain);
for(int i=0; i<3;i++)
{
View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false);
child.setId(i);
item.addView(child);
}
My problem is that #firstLayout displays properly, but #tView doesn't show up at all. Can someone please explain what I'm missing?
Thanks in advance.
I figured it out, it was simply an issue of setting the "android:layout_height" attribute equal to wrap_content on the #id/tView xml file. Silly little mistakes...