I made a tab menu using several fragments. And I'm going to put a cardview in each of the Fragment.
I tried Googling, but all I had to do was put a cardview in activity.
I'd like to put a cardview in the Fragment, and then I'd like to implement initiation of a predefined set activity when I click on the card.
How can I do?
I referred to https://www.codingdemos.com/android-tablayout-example-viewpager to tab menu.
I attach my fragment layout(xml file) code
<FrameLayout 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="com.aeyoung.csw.CommunityFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.constraint.ConstraintLayout 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"
tools:context="com.aeyoung.csw.CommunityFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
Also I add fragment code(java file)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class CommunityFragment extends AppCompatActivity {
//Create parameter
List<Product> productList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_community);
// initialize parameter "productList"
setInitialData();
//Find RecyclerView from fragment_community.xml
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
// Create LinearLayoutManager and set it to RecyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
//Create MyAdapter object with the parameters and set to RecyclerView
MyAdapter myAdapter = new MyAdapter(this, productList);
recyclerView.setAdapter(myAdapter);
}
private void setInitialData() {
productList.add(new Product("text1", "text1", R.mipmap.ic_launcher));
productList.add(new Product("text2", "text2", R.mipmap.ic_launcher));
productList.add(new Product("text3", "text3", R.mipmap.ic_launcher));
productList.add(new Product("text4", "text4", R.mipmap.ic_launcher));
productList.add(new Product("text5", "text5", R.mipmap.ic_launcher));
productList.add(new Product("text6", "text6", R.mipmap.ic_launcher));
productList.add(new Product("text7", "text7", R.mipmap.ic_launcher));
productList.add(new Product("text8", "text8", R.mipmap.ic_launcher));
productList.add(new Product("text9", "text9", R.mipmap.ic_launcher));
productList.add(new Product("text10", "text10", R.mipmap.ic_launcher));
}
}
PageAdapter.java code
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by abdalla on 2/18/18.
*/
public class PageAdapter extends FragmentPagerAdapter {
private int numOfTabs;
PageAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MajorFragment();
case 1:
return new FresherFragment();
case 2:
return new CommunityFragment();
case 3:
return new SettingsFragment();
default:
return null;
}
}
#Override
public int getCount() {
return numOfTabs;
}
}
And the following error occurs when building.
"error: incompatible types: CommunityFragment cannot be converted to Fragment"
as the error is telling you "error: incompatible types: CommunityFragment cannot be converted to Fragment"
you CommunityFragment extends AppCompatActivity and it's not a fragment.
Related
I've been trying to figure out this problem but I am new to Android Studio, when I try to run the app it displays this message:
Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
Here's my Main Activity file which is the cause :
package com.sti.lazshop;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.sti.lazshop.databinding.ActivityMainBinding;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
ArrayList<ItemModel> itemModels = new ArrayList<>();
int[] itemImages = {R.drawable.item_huion, R.drawable.item_iphone};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
RecyclerView recyclerView = findViewById(R.id.mRecyclerView);
Item_RecyclerViewAdapter adapter = new Item_RecyclerViewAdapter(this, itemModels);
setItemModels();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
replaceFragment(new HomeFragment());
binding.bottomNavigationView2.setOnItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.home:
replaceFragment(new HomeFragment());
break;
case R.id.cart:
replaceFragment(new CartFragment());
break;
case R.id.account:
replaceFragment(new AccountFragment());
break;
}
return true;
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
private void setItemModels() {
String[] itemNames = getResources().getStringArray(R.array.item_full_txt);
String[] itemPrices = getResources().getStringArray(R.array.item_prices);
for (int i = 0; i < itemNames.length; i++) {
itemModels.add(new ItemModel(
itemNames[i],
itemPrices[i],
itemImages[i]));
}
}
}
Here's the HomeFragment.java :
package com.sti.lazshop;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
Lastly, the fragment_home.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
There's a similar question to this but I don't know how to apply it and can't find a solution at the moment. I used a bottom navigator to navigate between fragments where fragment_home.xml recycler view is included. Thank you!
As I understand from fragment_home.xml You have a RecyclerView inside of HomeFragment xml layout. But you are tryin to find the View inside MainActivity.
The issue in your MainActivity's onCreate method.
When you call :
RecyclerView recyclerView = findViewById(R.id.mRecyclerView);
From MainActivity It's trying to find a View with id: R.id.mRecyclerView in you activity_main.xml(xml layout that you're using for MainActivity). But as I understand it's absent there, because it's inside fragment_home.xml.
Your can read how find View::findViewById works here:
https://developer.android.com/reference/android/view/View#findViewById(int)
Finds the first descendant view with the given ID, the view itself if
the ID matches getId(), or null if the ID is invalid (< 0) or there is
no matching view in the hierarchy.
So, When it can't find the view by id, it returns null.
Then you try to set Adapter to RecyclerView's variable which actually is null.
recyclerView.setAdapter(adapter); //recyclerView is null here
That's the reason of your issue.
You have 2 options.
Move RecyclerView from fragment_home.xml to activity_main.xml, than it will be worked.
Another option more suitable in my opinion, since you've already used fragments:
Move your code related to RecyclerView handling to HomeFragment class and call findViewById inside HomeFragment.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
Working on an android project just for the fun of it. I am getting back the error after a user tries to log in. I did a try-catch around the line where the error is and the RecyclerView object itself is null. So now that I know that, I am trying to figure out why it isn't getting set when I do findViewById(). I will paste the relevant code down below.
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
MainActivity.java
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.SearchView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
MyProductAdapter myProductAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
RecyclerView recyclerView;
recyclerView = findViewById(R.id.listId);
System.out.println("This is recyclverView" + recyclerView);
try {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
} catch (NullPointerException e) {
System.out.println("null here" + e.getCause());
}
ArrayList<MyProduct> myProductData = new ArrayList<>();
myProductData.add(new MyProduct("Panadol", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("Nyquill", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("ZAck", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("Codeine", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductAdapter = new MyProductAdapter(myProductData, MainActivity.this);
recyclerView.setAdapter(myProductAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bar, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
myProductAdapter.getFilter().filter(s);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
fragment_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".ui.dashboard.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
/>
</LinearLayout>
DashBoardFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.medistak.R;
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
}
});
return root;
}
}
Edit (after OP adds the DashboardFragment class)
Move your MyProductAdapter and recyclerView code from MainActivity to DashboardFragment onCreateView() method as follows:
RecyclerView recyclerView = findViewById(R.id.listId);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myProductAdapter);
Your RecyclerView listId resides in your DashboardFragment whereas you are initializing and setting it up in your MainActivity class. The reason why Null Object Reference error is showing up is because of the order in which Activities and their underlying Fragments are added to the Backstack.
First your MainActivity is setup. Then follows the Fragments lying in MainActivity, in your case its the DashboardFragment. So if you will setup a recyclerView of the DashboardFragment which is not even in existence from MainActivity's point of view and yet to be created , of course it will lead to a NPE.
The solution to your problem will be to pluck out all the recyclerView setup code from MainActivity class and transfer it over to DashboardFragment.
I'm beginner in Android development &
I am making a calculator app and want to update history in second fragment but not able to find a perfect solution for updating ListView in second fragment.
I tried many solutions but none of them worked properly.
I'm using ViewPager to swipe between calculator and history fragments.
I tired bundle class but if I use bundle in OnCreat method of first Fragment(CalculatorFragment) with ViewPager it shows me blank screen after starting an App & if I use Bundle class in Equal Button it crashes app.
here I am passing id of viewPager as Container of fragments.
what I want to achive.
I want to update history in second fragment(HistoryFragment) when ever "=" button is clicked.
here's a sample code of what I have done in my code so far.
MainActivity.java
package com.example.calculatorslide;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
pageAdapter pageAdapter;
#RequiresApi(api = Build.VERSION_CODES.M)
#SuppressLint({"SetTextI18n", "UseCompatLoadingForDrawables"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
//Get rid of ActionBar
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.hide();
pageAdapter = new pageAdapter(getSupportFragmentManager(), 2);
viewPager.setAdapter(pageAdapter);
getSupportFragmentManager().beginTransaction().add(R.id.ViewPager,
new Calculatorfragment()).commit;
}
}
Example of what I am doing in when Equal button is clicked in CalculatorFragment
public void Equals() {
String calc = etCalc.getText().toString();
if (calc.split("\\+").length == 2) {
String[] calculation = calc.split("\\+");
String Ans = String.valueOf(Double.parseDouble(calculation[0]) + Double.parseDouble(calculation[1]));
etCalc.setText(Ans);
tvCalc.setText(calc);
HistoryFragment fragment = new HistoryFragment();
Bundle bundle = new Bundle();
bundle.putString("key", calc+"="+Ans);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.ViewPager, fragment).commit;
}
}
HistoryFragment.java
package com.example.calculatorslide;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class HistoryFragment extends Fragment {
ListView history;
ArrayList<HistoryList> historyLists;
String History="";
View rootView;
public View onCreat(Bundle savedInstanceState){
Bundle bumdle = getArguments();
if (bundle != null)
String value = bundle.getString("key");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ArrayList<HistoryList> historyLists = new ArrayList<>();
if(History.split("=").length==2){
historyLists.add(new HistoryList(History.split("=")[0],History.split("=")[1]));
}
historyLists.add(new HistoryList("Example", "Example"));
historyLists.add(new HistoryList("23+5", "28"));
HistoryAdapter adapter = new HistoryAdapter(getActivity(), historyLists);
rootView = inflater.inflate(R.layout.fragment_history, container, false);
ListView listView = rootView.findViewById(R.id.history);
listView.setAdapter(adapter);
return rootView;
}
}
here in historyLists taking two parameters for calculation and answer to display in ListView.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
What I am trying to do here is to display a list of strings using ArrayList<String> and ArrayAdapter<String> when a user clicks a button. I want to declare all the members i.e. adapter, ArrayList and the list layout as global because I want to add more buttons later with the same feature of displaying a list of strings.
This code has no error but it's not working. I put the Toast in the onClick to make sure the onClick is working. I can see the toast but not the listView I want to see.
class file
R.id.button_news is the button id. R.layout.activity_primary_content is the layout that I'm using in this class PrimaryContent
R.layout.list_view_secondary is the layout where the listView R.id.list_view is located.
R.layout.list_view_secondary layout and the PrimaryContent class are not related but I want to use the listView which is in the list_view_secondary layout to display from the PrimaryContent.
In this line of code
arrayAdapter = new ArrayAdapter<>(view.getContext(),android.R.layout.simple_list_item_1,list); I tried by put the context view.getContext() and getBaseContext() both of them are not working.
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class PrimaryContent extends AppCompatActivity {
private final String LOG_TAG = PrimaryContent.class.getSimpleName();
public ListView listView;
public ArrayList<String> arrayList;
public ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary_content);
final LayoutInflater factory = getLayoutInflater();
final View rootView = factory.inflate(R.layout.list_view_secondary, null);
listView = rootView.findViewById(R.id.list_view);
arrayList = new ArrayList<>();
for (int i=0; i<20; i++) {
arrayList.add("World News");
}
Button news = findViewById(R.id.button_news);
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),"Damn",Toast.LENGTH_SHORT).show();
for (String x: arrayList) {
Log.v(LOG_TAG,x);
System.out.println();
}
arrayAdapter = new ArrayAdapter<>(view.getContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
}
});
}
}
R.layout.list_view_secondary
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
You forgot to add the inflated layout to your parent layout, for example:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent_layout);
parentLayout.addView(rootView);
I have this mainactivity:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//Array of options --> ArrayAdapter --> ListView
//List view: {views: list_items.xml}
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
//Create list of items
String[] myItems = {"Blue", "Green", "Purple", "Red"};
//Build Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this //Context for the activity
R.layout.list_items, //Layout to use (Create)
myItems); //Items to be displayed
//Configure the ListView
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
ViewPager myViewPager = (ViewPager) findViewById(R.id.viewPager);
myViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
myViewPager.setCurrentItem(1, false);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 2: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 0: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 3;
}
{}}}
I am trying to add a listview, i have created a xml file called: list_items.xml, but on this line: "R.layout.list_items," i get an error: "layout cannot be resolved or is not a field" and i also get "syntax error on token "R", delete this token". Why is that?
The list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
I believe that you misunderstood the constructor for ArrayAdapter.
What you basically need to do is change the R.layout.list_items to a single element layout, just like one you would find in android.R.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
Here you can see that first parameter is context, second one is single item layout, third (optional) is the ID of the text view you want to target with your collection of strings and the last one is the collection.
You can use your own layouts just like you tried earlier.
If you have any questions I will be happy to answer them, and also supply some code if needed.
It look error in your xml.You can also use the simple list item if you need to display only myItems strings.
String[] myItems = {"Blue", "Green", "Purple", "Red"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myItems);