ListView causes NullPointerException - java

This is my first Android app, i am trying to put a listview on and i have followed numerous tutorials but i always get the same result, a crash upon starting the app.
XML
<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:background="#drawable/background"
android:gravity="top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mattigins.mediacenter.MainActivity$PlaceholderFragment" >
<View
android:id="#+id/view1"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/View01"
android:background="#drawable/semi_trans_box" />
<View
android:id="#+id/View01"
android:layout_width="550dp"
android:layout_height="wrap_content"
android:layout_marginRight="58dp"
android:layout_toLeftOf="#+id/view1"
android:background="#drawable/semi_trans_box" />
<ListView
android:id="#+id/List1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/View01"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/View01" >
</ListView>
Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ListView listView = (ListView) findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);
}

Looks like your ListView belongs to the Fragment layout. Looking at
In onCreateView of Fragment.
ListView listView = (ListView) rootView.findViewById(R.id.List1);
String[] test_items = {"1","2","3","4"};
ArrayAdapter<String> test_adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test_items);
listView.setAdapter(test_adapter);

Related

Views not showing when added on button click?

Ok, I have been on this for 6 hours now and for some reason, I just don't get it.
I am trying to make an app to calculate something using a certain formula.
Now consider it as a grade point average calculating thing. I have a button which should allow the user to add two more spinners to the layout and thereby proceeding further. Now I made a method for this purpose. When I call this method for the first time the spinners are added. But it doesn't work the second time.
It also doesn't work if I put it in the button onClick() method. I have gone through multiple examples posted online and also on stackoverflow, maybe I am making some stupid mistake. Just guide me through this, please.
Here is the code:-
SGPA.class
public class SGPA extends AppCompatActivity {
View v;
Context c;
LinearLayout subjectData;
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
//int id = View.generateViewId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sgpa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
//TODO if 2015 or 2013 regulations
//assuming 2015
fill2015();
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sgpa, null);
subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
c = this;
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addViewForAnotherSubject();
setContentView(v);
}
});
setContentView(v);
}
void addViewForAnotherSubject()
{
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
subjectData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
void fill2013()
{
}
}
activity_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="innominatebit.srmite.SGPA">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_sgpa" />
</android.support.design.widget.CoordinatorLayout>
content_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="6dp"
android:paddingEnd="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingStart="6dp"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="innominatebit.srmite.SGPA"
tools:showIn="#layout/activity_sgpa">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/subjectdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
All help is appreciated. Thanks in advance!
Ok, I got it. i made another test application for debugging purposes until I got it. Seems I did not need a layout inflator at all.
Here's the code:
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
fill2015();
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
final LinearLayout myData=(LinearLayout)findViewById(R.id.myData);
addforanothersubject(myData);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addforanothersubject(myData);
}
});
}
void addforanothersubject(LinearLayout myData)
{
Context c=MainActivity.this;
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
myData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
}
And the xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="aaa.test.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/myData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Hope it helps someone.

Set ListView Adapter for ListView that is in a Fragment from an Activity

I need to set a ListView's Adapter which is located in a Fragment from an Activity.
I have tried to set the Adapter from the Activity with the code below, but it returns a NullPointerException, presumably because the Fragment has not been loaded yet.
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
Is there a way to run the above code after the Fragment has loaded in, or a better way of doing this altogether? I have attempted to use a method that would set the Adapter inside the Fragment itself, but I still need to wait for the fragment to be loaded before I instigate it from the Activity or I will still receive a NullPointerException. I would also need to put an ArrayList<HashMap<String, String>> into a bundle, which there is no documentation on how to do so if I was going to use that method.
I have also tried to instigate a method that holds the set adapter code that is located inside the Activity from the Fragment's onActivityCreated() method which still returned a NullPointerException.
Am I not referencing the ListView element correctly from the Activity or is there a way to wait for the Fragment to load in, so I don't receive an error?
TabbedActivity.java / Main Parent Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(6);
mViewPager.setAdapter(mSectionsPagerAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// ...
interpretGrades(retrieveGrades());
}
public void interpretGrades(String input) {
// take CLASSES~~MAINCLASSDIVIDER~~GRADES
// turn it into Classes = Classes
// Grades = Grades
final String[] ServerResponse = input.split("~~MAINCLASSDIVIDER~~");
final String[] Classes = ServerResponse[0].split(";");
final String[] Grades = ServerResponse[1].split(";");
ArrayList<HashMap<String, String>> item_list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Classes.length; i++) {
HashMap<String, String> item = new HashMap<String, String>();
String[] ClassSplit = Classes[i].split("-");
String Period = ClassSplit[0];
String Class = ClassSplit[1];
item.put("Period", Period);
// --.substring(1, --.length()) removes first character from string (in this case, a whitespace from the class name)
item.put("Class", Class.substring(1, Class.length()));
item.put("Grade", Grades[i]);
item_list.add(item);
}
String[] list_input = new String[] { "Period", "Class", "Grade" };
// PUT THE STRING VALUES FROM list_input INTO THE XML LAYOUT VALUES
int[] layout_values = new int[] { android.R.id.text1, android.R.id.text2, R.id.grade};
// LISTVIEW LAYOUT (XML FILE)
int list_layout = grade_list;
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
}
Tab2Pulse.java / Fragment
public class Tab2Pulse extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.tab2chat, container, false);
return mainView;
}
}
tab2chat.xml / Fragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab2chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/gradeListView"
style="#android:style/Widget.Material.Light.ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
activity_tabbed.xml / Main Activity Layout
<android.support.design.widget.CoordinatorLayout
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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/main_gradient"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<View
android:layout_width="match_parent"
android:layout_height="12dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/main_gradient"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white" />
</android.support.design.widget.AppBarLayout>
<View
android:id="#+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_below="#id/toolbar"
android:background="#drawable/toolbar_dropshadow" />
<!--<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="368dp"
android:layout_height="495dp">-->
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="#*android:dimen/navigation_bar_height"
android:background="#drawable/login_gradient" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Does my adapter work?

I get inputs from the user and then put them in a ListView, but when I click on the button, it works with no errors, opening the layout and immediately closing it. When I try with arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_list);, it works perfect.
public class HomeChat extends AppCompatActivity {
ListView listView;
EditText writeSms;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> m_list = new ArrayList<String>();
String emriUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_chat);
writeSms = (EditText) findViewById(R.id.shkrunSms);
listView = (ListView) findViewById(R.id.listView);
emriUser = getIntent().getExtras().getString("emri");
arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_chat,m_list);
listView.setAdapter(arrayAdapter);
}
public void sentSmsButton(View view) {
String mesazhet = emriUser + ": " + writeSms.getText().toString();
if (writeSms != null && writeSms.length() > 0) {
m_list.add(mesazhet);
arrayAdapter.notifyDataSetChanged();
writeSms.setText("");
}else
{
Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_LONG).show();
}
}
}
Here is the chat layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
</LinearLayout>
The problem is that ArrayAdapter is expecting a layout with only a single TextView in it. So, your layout with a LinearLayout containing a TextView won't work. Try changing R.layout.list_chat to simply:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>

Add a Spinner to a RelativeLayout dynamically

I am trying to add a spinner dynamically to a RelativeLayout but I am having problems.
This is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/Relativ"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
And in my class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Relativ);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,1);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
Spinner list = new Spinner(this);
list.setAdapter(adapter);
rl.addView(list);
}
But it does't run. I would like to know what is wrong in my code and how I can fix it.
I have tryed with RelativeLayout params in the RelativeLayout.addView and it doesn't run.
Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = new RelativeLayout(this);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Spinner list = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, arrayList);
list.setAdapter(adapter);
rl.addView(list);
setContentView(rl);
}
main.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
final Spinner list= (Spinner) findViewById(R.id.spinner1);
list.setAdapter(adapter);
}
main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.prova.MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
</RelativeLayout>
Check with your parameters.

Android nullpointerException - listview

I've seen other people who have had the same issue, but they were missing setContentView().
Any ideas as to why the below isn't working? NPE occurs when I call setAdapter because listView is null.
public class DisplayMessageActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
ArrayList<String> message = getArray();
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, message);
listView.setAdapter(adapter);
};
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<ListView android:id="#+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
You are referencing R.layout.activity_display_message when you call setContentView meaning that (ListView) findViewById(R.id.listView); will return null
Instead of setContentView(R.layout.activity_display_message) call setContentView(R.layout.activity_main)

Categories

Resources