android.widget.LinearLayout cannot be cast to android.widget.TextView (android-studio) - android

Im Trying To paste data(from an Intent) on a customized ListView ,and then I get this error,but when I did it with an uncustomized ListView it worked properly.
I understood that the problem occurs when I change :
ListAdapter theAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,timegone);
to:
ListAdapter theAdapter=new ArrayAdapter<String>(this,R.layout.row_layout,timegone);
But I dont get why it doesnt work!
Here below I added the row_layout.xml file which Causes the app crash.
Thats how it works :
public class Shifts extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
Intent MyIntent=getIntent();
String timepass=MyIntent.getExtras().getString("time");
ListView thelist=(ListView)findViewById(R.id.MyListView);
String[] timegone=timepass.split(":");
ListAdapter theAdapter=new ArrayAdapter<String>(this,**android.R.layout.simple_list_item_1**,timegone);
thelist.setAdapter(theAdapter);
}
}
And thats how it doesnt work:
public class Shifts extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
Intent MyIntent=getIntent();
String timepass=MyIntent.getExtras().getString("time");
ListView thelist=(ListView)findViewById(R.id.MyListView);
String[] timegone=timepass.split(":");
ListAdapter theAdapter=new ArrayAdapter<String>(this,**R.layout.row_layout**,timegone);
thelist.setAdapter(theAdapter);
}
}
This is the row_layout.xml that im using in the adapter
<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"
tools:context=".MainScreen"
android:id="#+id/ly"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:textSize="30sp"
android:textStyle="bold"
android:padding="15dp"/>
</LinearLayout>
What should I do in order to fix this ?
Thank you!

You need to create custom adapter if you want custom layout of list row..
here you can see the example.

Related

Hitting URL to get data and display data in list view [duplicate]

I am getting a run time exception
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
I don't know what is wrong.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslist);
mDbHelper.open();
fillData();
}
private void fillData() {
Bundle extras = getIntent().getExtras();
long catID = extras.getLong("cat_id");
Cursor c = mDbHelper.fetchNews(catID);
startManagingCursor(c);
String[] from = new String[] { DBHelper.KEY_TITLE };
int[] to = new int[] { R.id.newslist_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.text_newslist, c, from, to);
setListAdapter(notes);
}
newslist.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">
<ListView
android:id="#+id/catnewslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
text_newslist.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:text="#+id/newslist_text"
android:id="#+id/newslist_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
This will solve the error if you still want to use a ListActivity.
Remove the call to setContentView - you don't need it in a ListActivity unless you're doing something radical. The code should work without it.
Another way is, don't extend ListActivity. Just extends Activity, then you can create your list view by setContentView() and get the list view by findViewById(R.id.yourlistview).
If you extends from ListActivity, then don't use setContentView(). You need get the default list view hosted in list activity by getListView().
I had a similar issue with the error message you received, and I found it was because I was extending ListActivity instead of just Activity (that's what I get for re-purposing code from a different project ;) )
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
I face like this too. In my case (Maybe not relevant with your case, just share to others) in class mainActivity.java
yourClassName extends ListActivity(){...} change to yourClassName extends Activity(){...}
I also faced this issue, I was extending my activity class from listactivity, but the id of my list view was not defualt I changed it back to list and now its working fine.
Asim soroya

NullPointerException when trying to load a button

I am trying to open a new activity. When I do I get a null pointer exception. Everything works untill the following code gets executed.
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
Here is a better look at the code.
public class DisplayTests extends ListActivity {
private DbManagement mdbManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
private OnClickListener AnsRmTest = new OnClickListener(){
#Override
public void onClick(View v) {
try{
System.out.println("fart");
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
I am not sure why the activity would bomb out when it reaches the creation of a button.
Here is the XML as well
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="194dp" >
</ListView>
</FrameLayout>
<Button
android:id="#+id/btnNewExam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/strNewExam" />
<Button
android:id="#+id/btnRmExams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnRmTests"
android:layout_alignBottom="#+id/btnRmTests"
android:layout_alignParentRight="true"
android:text="#string/strRmExams" />
<Button
android:id="#+id/btnRmTests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnNewExam"
android:layout_alignBottom="#+id/btnNewExam"
android:layout_toLeftOf="#+id/btnRmExams"
android:text="#string/strRmTest" />
</RelativeLayout>
So with that being said I am the following code
setContentView(R.layout.moretime);
When I did that I get the following error:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
I do have a list view as you can see about but it is called listView1 and I am not sure why I am getting this error message. Thanks in advance.
You need to set the content of your layout to the activity first then initialize views.
privae Button RmTests;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // missing
RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
... // rest of the code
}
You can findViewById of the current view hierarchy set to the activity. Since you have not set the content to the activity, when you initialize your button you get NullPointerException.
ALso
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
In your xml replace
<ListView
android:id="#+id/listView1"
By
<ListView android:id="#android:id/list"
Also do you require a FrameLayout? Why not have just the listview and the buttons in the relative layout.
http://developer.android.com/reference/android/app/ListActivity.html
Edit:
From the comment below
You can do as below
public class MainActivity extends ListActivity {
String s[] = {"A","B","C","D","E"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, s);
setListAdapter(adapter);
}
}
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate().
If you need to have other views then you can define them in xml extend ListActivity in which case you need to have listview with id "#android:id/list". Like i can have a list and a button at the button. So i need to have a listview with id as "#android:id/list".
Where do you tell your activity which xml it is supposed to load?
Try:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
replace "R.layout.main" with your xml file title. E.g. if it's Main_Layout.xml, replace it with "R.layout.Main_Layout"
Write
setContentView(R.layout.your_layout);
after
super.onCreate(savedInstanceState);
try be adding setContentView(R.layout.your_layout_name); after super.onCreate(savedInstanceState);
You have to set the setContentView .Here is the Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
}
I think in you Activity inside onCreate methode you forget to give setContentView
Before initializing the UI components you must set view for activity inside onCreate methode
ie setContentView(R.layout.yourxml)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxml);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
Additional Info
also since your are using ListActivty you must provide listview id as
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="194dp" />
Otherwise it will cause another error

View is null even after initialization

I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method

Display contents from ListView defined in regular Activity Android

I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.

How to use R to select a ListView when using it with ListActivity

I have this code:
public class MyActivity extends ListActivity implements OnClickListener {
private ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setAdapter();
this.bindButtons();
}
private void setAdapter() {
setContentView(R.layout.siteactivity);
adapter=new ArrayAdapter<String>(this,
R.layout.siteitem,
listItems);
setListAdapter(adapter);
}
private void bindButtons() {
findViewById(R.id.buttonPrevious).setOnClickListener(this);
findViewById(R.id.buttonNext).setOnClickListener(this);
}
// ...
}
with this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/siteActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/navigation">
<Button
android:text="<="
android:textSize="12dp"
android:id="#+id/buttonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="=>"
android:textSize="12dp"
android:id="#+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/navigation"
android:layout_above="#+id/transport_selection"
/>
</RelativeLayout>
Note the android:id="#android:id/list" of the ListView. If I replace it by android:id="#+id/list", my activity force closes, because my "content must have a ListView whose id attribute is 'android.R.id.list'", but this is the expected behaviour, I think.
Now, I want to add a context menu to the items of the ListView. I tryed registerForContextMenu(findViewById(R.id.list));, but it doesn't work, because of the android:id.
Then, how can I add a context menu?
Regards,
As you are using ListActivity, you can use the following code to get list view in code:
ListView myListView=getListView ();
Use android.R.id.list instead of R.id.list.
(And by the way, the error message you wrote here turns out to have the answer)
Basically, anything defined in XML as #android:whatever is the same as android.R.whatever in the Java code. Anything that you defined in XML, which will look like #whatever, is the same as R.whatever in Java code.
Final edit: If you're using ListActivity, you are required to have a ListView with the id #android:id/list. Thus, unless you want to add another ListView to your activity, you shouldn't need #+id/list (or R.id.list).
You need to create an AlertDialog and call the dialog to show inside the listview's onItemLongClick() event. Click here for an example.

Categories

Resources