Reputation: 1591
I have made a simple android ListView demo but its not working my code is below:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
main.java
package com.example.mylist;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
static final String[] list = new String[] { "Awesome", "New", "Retro",
"Rock", "Pop", "Bollywood", "hollywood" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main,
list));
ListView listvw = getListView();
listvw.setTextFilterEnabled(true);
listvw.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
logcat
07-29 14:16:21.335: E/AndroidRuntime(27347): FATAL EXCEPTION: main
07-29 14:16:21.335: E/AndroidRuntime(27347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylist/com.example.mylist.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.os.Looper.loop(Looper.java:137)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-29 14:16:21.335: E/AndroidRuntime(27347): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:16:21.335: E/AndroidRuntime(27347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-29 14:16:21.335: E/AndroidRuntime(27347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-29 14:16:21.335: E/AndroidRuntime(27347): at dalvik.system.NativeStart.main(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-29 14:16:21.335: E/AndroidRuntime(27347): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
07-29 14:16:21.335: E/AndroidRuntime(27347): at android.app.Activity.setContentView(Activity.java:1862)
07-29 14:16:21.335: E/AndroidRuntime(27347): at com.example.mylist.MainActivity.onCreate(MainActivity.java:20)
pls help me...!
Upvotes: 0
Views: 527
Reputation: 666
package com.ListView;
public class ListViewActivity extends Activity implements OnItemSelectedListener{
/** Called when the activity is first created. */
private String[]
Name={"Destop","CPU","Pendrive","Mouse","Keyboard","Harddisk","Cardreader","MotherBord"};
ListView l1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.listView1);
l1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Name));
l1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, Name[arg2].toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "Item Not Selected", Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Reputation: 666
<?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_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
Upvotes: 0
Reputation: 7092
Just comment this line in your code. Then i hope it work
// setContentView(R.layout.activity_main);
and do not call onclick try this way
listvw.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();
}
});
and import this two
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
Upvotes: 1
Reputation: 23269
You're supplying a custom layout but not defining a ListView
in it.
Change your main.xml
(I assume it's either main.xml
or activity_main.xml
, your code refers to one but you post the other) to:
<?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="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" />
http://developer.android.com/reference/android/app/ListActivity.html
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)
Alternatively, as the documentation suggests, remove the setContentView()
call if you have no plans to customise the layout.
Upvotes: 0