I am starting out on Android and trying to make a custom ListView layout. I've followed some guides and have made the following code:
public class CheckInList extends ListActivity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
mAdapter = new ArrayAdapter<String>(this, R.layout.checkinlist_item, R.id.checkinlist_item_text, mNames);
setListAdapter(mAdapter);
...
}
}
This is the code for checkinlist_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/checkinlist_item_bg">
<TextView android:id="#+id/checkinlist_item_text"
style="#style/RegisterText" />
</RelativeView>
If I use android.R.layout.simple_list_item_1 instead of my above template then everything is fine and my ListView works, however whenever I use the above code my activity crashes. I am running things on Android 1.5.
Any ideas why things are crashing?
Could it be because RelativeView does not exist?
I might suggest adding the attributes android:layout_width="fill_parent" and android:layout_height="fill_parent" or set them to whatever you want, but those are required. And yes, use RelativeLayout as Matthew said, since RelativeView doesn't exist.
at first sight there is no width/height values for your textView ?
Related
I'm following this online tutorial to build a part of my app that will return a list of my contacts, but I've run in to a bit of an issue that doesn't seem to be mentioned anywhere in the tutorial.
This is in the class ContactsFragment, which is not the main activity of the project but returns the fragment for the main activity to use.
The error is occurring here...
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContactsList =
(ListView) getActivity().findViewById(R.layout.contacts_list_view); <--- here
//...
}
The error reads "expected resource of type id", which is strange because Android Studio's intellisense is suggesting contacts_list_view to me when writing the code.
contacts_list_view.xml is a file in the res/layout directory that I have copied from the tutorial. Its contents is as below:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I'm pretty new to Android Studio and Android development in general, so there's a chance I'm missing something obvious. Feel free to point that out to me, or anything else that might fix my issue!
Thanks,
Mark
Change this line in the code:
(ListView) getActivity().findViewById(R.layout.contacts_list_view); for (ListView) getActivity().findViewById(R.id.list);
And in the .xml file:
android:id="#android:id/list" for android:id="#+id/list"
You call findViewById(R.layout.contacts_list_view); with the param R.layout.contacts_list_view but what you need there is an id of a view...what you need actually there is R.id.list
Try using, the warning will gone
setContentView(R.id.main_layout as Int)
Just initialize in inside OnViewCreated like this
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView mContactsList =(ListView) view.findViewById(R.layout.contacts_list_view);
}
I'm completely new to Android programming, so I'm not really sure what I should be searching for. I have a LinearLayout element on one of my activities.
<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="wrap_content"
android:layout_marginTop="15dp"
android:id="#+id/comment_area"
android:orientation="vertical"/>
I have a JSON array of comments (userName, commentText, commentDate) that I want to add into this LinearLayout though a loop. I created a comment_view.xml layout and created a CommentWidget class extending LinearLayout. Frankly I have no idea if this is the correct approach, and I don't think it is because I can't get the comments to load in.
My class is
public class CommentWidget extends LinearLayout {
private String text;
public void setText(String text) {
this.text = text;
}
public CommentWidget(Context context){
super(context);
}
public CommentWidget(Context context,AttributeSet attrs){
super(context,attrs);
}
#Override
protected void onFinishInflate(){
super.onFinishInflate();
TextView textView=(TextView) findViewById(R.id.comment_text);
textView.setText(text);
}
}
My widget layout is
<?xml version="1.0" encoding="utf-8"?>
<com.myproject.CommentWidget xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/comment_text"/>
</com.myproject.CommentWidget>
Inside my loop on the activity I was calling:
CommentWidget w = new CommentWidget(this);
w.setText(comment.getText());
mtxtArea.addView(w);
But nothing shows up. Can someone point me in the right direction? I'm correctly receiving the JSON into an array already.
Update: Answer
Windsurfer's answer below set me on the right track to use a ListView for what I am trying to accomplish. Using his links and some searching I found out that extending the ArrayAdapter is the most appropriate for JSON type data. I ended up following the tutorial at the following link
https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
You can very well extend the LinearLayout to do this, but Android already has a couple of widgets designed for this. I believe you're looking for a ListView to display an array of data. Rather than creating a new widget, take a look at how a ListView works. A ListView uses an adapter to bind data to it. You will still have to design the layout for a single comment item, but a lot of the heavy lifting is done by the ListView and it's adapter.
Here are some links to get you started:
http://developer.android.com/guide/topics/ui/layout/listview.html
http://www.vogella.com/tutorials/AndroidListView/article.html
Take a look at this link by Romain Guy who introduces ListViews too.
If I get the error "android.content.res.Resources$NotFoundException: Resource ID #0x7f050007 type #0x12 is not valid" can I find some what this resource is if I know its ID?
ListView list = (ListView)findViewById(R.id.messages_list_view);
list.setAdapter(new ArrayAdapter<String>(context,
R.layout.messages_list, headers));
messages_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/messages_list_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/messages_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I Got this error when using ListView in a Fragment.
Resolved by moving the setAdapter lines to the onViewCreated function of the Fragment. (makes sense that before the view is created the ListView is invalid).
so you get :
public void onViewCreated(View view,Bundle savedInstanceState){
ListView list = (ListView) getView().findViewById(R.id.thelist);
list.setAdapter(mAdapter);
}
For those whom other mentioned solutions don't work.
I did this silly mistake:-
setContentView(R.id.something);
Instead of
setContentView(R.layout.something);
Corrected that, and error was gone :D
You can either use the search function in eclipse, search for "0x7f050007" or go to projectfolder/gen/path/R.java that contains your resources.
You'll find something like this:
public static final int lineItem=0x7f07001c;
Then search for(in this example) lineItem with Eclipses search function. It'll take you to your resource in code.
Check your imports (at the top of your class-file). Maybe you imported
android.R
(which provides access to the platform-resources) instead of
{your_package_name}.R
(you can also leave it blank).
I have the following code:
public class Level extends Activity{
public static String TAG="Level";
Level_Score_bar score_bar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level_layout);
score_bar=(Level_Score_bar) findViewById(R.id.Score_Bar);
Log.d(TAG,"score_bar="+score_bar);
}
}
The XML code looks like this (R.layout.level_layout) (Non-important stuff removed)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<pearsonartphoto.AJEG.Level_Score_bar android:id="#+id/Score_Bar" android:layout_height="wrap_content" android:layout_width="fill_parent">
</pearsonartphoto.AJEG.Level_Score_bar>
</LinearLayout>
The log statement reads "score_bar=null".As you can see, there is a resource named R.id.Score_Bar. Am I missing something? I need to use the function for some other stuff. Thanks!
There are a number of things that can cause this. The most common being that for one reason or another R.id was not generated correctly. Usually doing a simple 'clean' will fix this. If that doesn't work another thing to check would be the constructor for the custom view. You can find this and other suggestions from this question.
The setText() method returns null in my application why?
public class GetValue extends Activity {
char letter = 'g';
int ascii = letter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.txt_1);
textView.setText(ascii);
}
}
It doesn't matter what text i put in, it crashes anyway. Why does setText() keep returning null?
Thank you, in advance
Solution: My error was in the xml file. I wrote:
android:text="#+id/txt_1"
When it should say:
android:id="#+id/txt_1"
Thanks a lot for all the answers and comments!
You tried to pass an integer as parameter to setText, which assumes it is a resource ID. To display computed text, pass it as a string: textView.setText("g");
Edited: Check your XML file, I have test with something very basic and it works
<?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">
<TextView
android:id="#+id/txt_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="myTextView"/>
</LinearLayout>
Maybe try to clean your project (Project->Clean in Eclipse), I recently have some trouble with R generation on the last ADT version.
Try this:
textView.setText(Integer.toString(ascii));
Also make sure that your layout xml has TextView txt_1
I've tried :
Integer.toString(char) and String.valueOf(char)
both didnt work.
The only solution is :
txt.setText(""+char);
This is not very efficient from optimization point of view but it does the trick :)