All Objects within a fragment returning null - android

My fragment is not initialising objects correctly. I'm struggling to see why.
Many StackOverflow questions have all suggested adding the following line in the fragment's OnCreateView() should initialise objects correctly.
mList = (ListView) view.findViewById(R.id.locationList);
It doesn't seem to be working, as I can see in debug that my Switch and ListView are returning as null. Within my fragment's xml, I have a Switch and ListView id's of lostToggle and locationList appropriately, so they should point to these objects. I'm struggling to see why this is happening. Any help is appreciated.
page2_fragment.java
public class page2_fragment extends Fragment {
private Switch mToggle;
private ListView mList;
private ArrayAdapter<String> mLocationArrayAdapter;
private ArrayList<String> items = new ArrayList<String>();
public page2_fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page2_fragment, container, false);
mToggle = (Switch) view.findViewById(R.id.lostToggle);
mList = (ListView) view.findViewById(R.id.locationList);
mLocationArrayAdapter = new ArrayAdapter<>(
getActivity().getApplicationContext(),
android.R.layout.simple_expandable_list_item_1,
items);
setupLostToggle();
// Inflate the layout for this fragment
return view;
}
public ArrayAdapter getArrAdapter() { return mLocationArrayAdapter; }
public ListView getList() { return mList; }
public ArrayList<String> getArrayList() { return items; }
public Switch getToggle() { return mToggle; }
private void setupLostToggle() {
//Add "Lost" toggle functionality
mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
Log.d("Toggle: ","Enabled");
} else {
// The toggle is disabled
Log.d("Toggle: ","Disabled");
}
}
});
}
}
page2_fragment.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.robertdavis.aloost.page2_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/t1_colorPrimaryDark"
android:baselineAligned="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/activity_vertical_margin"
android:text="#string/keyword_location"
android:textAllCaps="true"
android:textColor="#android:color/white"/>
</LinearLayout>
<android.support.v7.widget.CardView
android:id="#+id/main_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="8dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:gravity="top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="9dp"
android:layout_weight="0.25"
android:text="I'm looking for"/>
<Spinner
android:id="#+id/spinnerCategory"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="0.75"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:gravity="top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="within"/>
<Spinner
android:id="#+id/spinnerRadius"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="kilometres." />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:gravity="top"
android:orientation="horizontal">
<Button
android:id="#+id/button_getPlacesData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="getPlacesData"
android:text="Go"
android:textAllCaps="true" />
<Switch
android:id="#+id/lostToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="Lost? "
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/results_list_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
app:cardCornerRadius="4dp">
<ListView
android:id="#+id/locationList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
Edit: Logcat data (as requested)
12-10 06:57:20.088 18139-18139/com.example.robertdavis.aloost E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.robertdavis.aloost, PID: 18139
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.robertdavis.aloost.AsyncTaskParseJson.onPostExecute(AsyncTaskParseJson.java:95)
at com.example.robertdavis.aloost.AsyncTaskParseJson.onPostExecute(AsyncTaskParseJson.java:16)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

try initialising object in onViewCreated(View view, Bundle savedInstanceState)
method

Related

Why Custom ArrayAdapter not showing TextView in a ListView

I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_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"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>

Update Listview in android

I am trying to create an activity where the user can create some recipe and input the ingredients. every time the user enters an ingredient, it should be added to a listview. The issue I am having here is that the first input is the only one that is added to the listview not the others.
Here is the layout script:
<?xml version="1.0" encoding="utf-8"?><ScrollView
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=".Activities.AddActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/recipe_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/number_of_servings" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/preparation_time" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/calories" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/ingredients"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/round_add_circle_outline_black_36dp"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
And the logic behind it :
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredients.add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
public class IngredientAdapter extends ArrayAdapter<String> {
ArrayList<String> mNewIngredients;
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
mNewIngredients = objects;
}
public String getItem(int position) {
return mNewIngredients.get(position).toString();
}
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
if(convertView==null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient,parent,false);
TextView recipeName= convertView.findViewById(R.id.new_added_ingredient);
recipeName.setText(item);
return convertView;
}
}
}
Try to scroll down your list view, maybe in your item's layout, you set the layout_height = match_parent so you do not see the other inputs.
You need to add android:fillViewport="true" to your ScrollView
Also change all child LinearLayout height to wrap_content instead of match_parent
Try this
<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/app_name"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/ic_launcher_background"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
In your implementation of IngredientAdapter, you are using your own ArrayList mNewIngredients for the data instead of the collection in the ArrayAdapter. When you add new data into ingredients, it is not added to the adapter.
To make use of the collection implemented in ArrayAdapter for simplicity. The following is an example, with a view holder so that views can be reused.
public class IngredientAdapter extends ArrayAdapter<String> {
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient, parent, false);
holder = new ViewHolder();
holder.recipeName = convertView.findViewById(R.id.new_added_ingredient);
convertView.Tag = holder;
} else {
holder = (ViewHolder)convertView.Tag;
}
String item = getItem(position);
holder.recipeName.setText(item);
return convertView;
}
}
public class ViewHolder {
public TextView recipeName;
}
The data in the adapter is now backed by the internal collection. And in the activity, you should add the new data into adapter directly.
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
// keep this if you need it for something else
// private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// keep this if you need it for something else
// ingredients.add(new_ing.getText().toString());
// to add adata into the adapter
adapter.Add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
}

Change Fragment's TextViews's text from Activity

I want to change an textView's text via an Activity, but when I change by this code:
public class MainActivity extends AppCompatActivity {
private TextView txtSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSave = (TextView)findViewById(R.id.txt_save);;
}
public void onClick(View v) {
TextView txtSave;
txtSave = (TextView)findViewById(R.id.txt_save);
txtSave.setText("something");
}
}
and I got this error:
05-14 13:47:46.835 21949-21949/stv.wordspower E/AndroidRuntime: FATAL EXCEPTION: main
Process: stv.wordspower, PID: 21949
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Can somebody add me help how can I change dynamically?
Fragment code:
public class ProgressFragment extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_progress, container, false);
return rootView;
}
public static ProgressFragment newInstance() {
Bundle args = new Bundle();
ProgressFragment fragment = new ProgressFragment();
fragment.setArguments(args);
return fragment;
}
}
The textView is on the ProgressFragment. The ProgressFragment is on the MainActivity's FrameLayout.
I just want to modify the TextView's values but I can't. I have tried these solutions, but one did not work.
other informations:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="stv.wordspower.MainActivity">
<TextView
android:text="#string/txtView_welcome"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<Button
android:text="#string/btn_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn_exit"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_settings"
android:layout_above="#+id/btn_contact"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btn_contact"
android:layout_above="#+id/btn_exit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:text="#string/btn_progress" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_progress"
android:layout_above="#+id/btn_settings"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/mainActivityFragmentLayout">
</FrameLayout>
<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="stv.wordspower.ProgressFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/txt_progresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/txt_progresses"
android:textAlignment="center"
android:textColor="#android:color/background_dark"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/txt_empty"
android:textAlignment="center"
android:textSize="30dp"
android:onClick="onClick"
android:clickable="true"/>
In onClick method, you can get fragment as following:
public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
}
and in fragment, create a method to update the textView, so that we can call it using fragment object as following:
public void update(String s){
textView.setText(s);
}
Note: This textView should be defined in your fragment.
and call this from main activity as following:
public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragment.update("foo");
}
And yes! if you are not using support library, just replace getSupportFragmentManager() by getFragmentManager().

Cannot set Custom Adapter to ListView

I try to set a Customer Adapter to ListView but always receive NullException.
This is my Adapter:
public class MovieAdapter extends BaseAdapter{
private Context mContext;
// Add mVar of resource, an array for example
private List<Movie> mMovies;
// Automatically create constructor for all mVar
public MovieAdapter(Context context, List<Movie> movies) {
mContext = context;
mMovies = movies;
}
public MovieAdapter(Callback<NowPlaying> callback, List<Movie> movies) {
}
// Automatically create required methods and override them with mVar
#Override
public int getCount() {
return mMovies.size();
}
#Override
public Object getItem(int position) {
return mMovies.get(position);
}
#Override
public long getItemId(int position) {
return 0; // Use to easier get position reference
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// If the ListView is brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_movie_item,
parent, false);
// Get layout from the context and inflate it the daily_list_item.
// Use ViewHolder to create smooth scrolling list
holder = new ViewHolder(convertView);
// Set the tag for reuse the View
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Set content to holderVar from get
//Create the ModelClass object as an element in the array
// Remember even the Image need to be set (different from MainAct)
Movie movie = mMovies.get(position);
holder.mTvLeftTitle.setText(movie.getTitle());
holder.mTvLeftOverview.setText(movie.getOverview());
// holder.mTvLeftCast.setText(movie.getCast());
Glide.with(mContext)
.load(movie.getPosterPath())
.into(holder.mImgLeftPoster);
holder.mTvRightTitle.setText(movie.getTitle());
holder.mTvRightOverview.setText(movie.getOverview());
// holder.mTvLeftCast.setText(movie.getCast());
Glide.with(mContext)
.load(movie.getPosterPath())
.into(holder.mImgRightPoster);
return convertView;
}
// Create class ViewHolder with Widget as variable based on Model class
static class ViewHolder {
// Using ButterKnife to create and hook Widget (remember the Image, too)
#BindView(R.id.rlLeftLayout)
RelativeLayout mRlLeftLayout;
#BindView(R.id.tvLeftTitle)
TextView mTvLeftTitle;
#BindView(R.id.tvLeftOverview)
TextView mTvLeftOverview;
#BindView(R.id.tvLeftCast)
TextView mTvLeftCast;
#BindView(R.id.imgLeftPoster)
ImageView mImgLeftPoster;
#BindView(R.id.rlRightLayout)
RelativeLayout mRlRightLayout;
#BindView(R.id.tvRightTitle)
TextView mTvRightTitle;
#BindView(R.id.tvRightOverview)
TextView mTvRightOverview;
#BindView(R.id.tvRightCast)
TextView mTvRightCast;
#BindView(R.id.imgRightPoster)
ImageView mImgRightPoster;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
This is my Activity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
MovieAdapter mAdapter;
List<Movie> mMovies = new ArrayList<Movie>();
private NowPlaying mNowPlaying;
#BindView(android.R.id.list)
ListView mListView;
#BindView(android.R.id.empty)
TextView mEmptyView;
private MovieApi mMovieApi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
// final MovieAdapter adapter = new MovieAdapter(this, mMovies);
//
mMovieApi = RetrofitUtils.get(getString(R.string.api_key)).create(MovieApi.class);
mMovieApi.getNowPlaying().enqueue(new Callback<NowPlaying>() {
#Override
public void onResponse(Call<NowPlaying> call, Response<NowPlaying> response) {
Log.d("Response", String.valueOf(response.isSuccessful()));
mMovies = response.body().getMovies();
mAdapter = new MovieAdapter(this, mMovies);
mListView.setAdapter(mAdapter);
mListView.setEmptyView(mEmptyView);
}
#Override
public void onFailure(Call<NowPlaying> call, Throwable t) {
Log.e("Error", t.getMessage());
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
});
}
}
My Stack Trace:
10-15 23:04:19.480 12537-12537/com.example.rubit1359.bigcornbox E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rubit1359.bigcornbox, PID: 12363
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.rubit1359.bigcornbox.ui.MainActivity$1.onResponse(MainActivity.java:57)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
I have debugged the app to make sure that the mMovies ListView are filled. However I cannot set the Adapter. Anyone can help me spotting the error.
Thank you.
UPDATE XML
XML activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkness"
tools:context="com.example.rubit1359.bigcornbox.ui.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/ListView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="There is no data to display"
android:textStyle="bold"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/colorWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/EmptyView"
/>
</RelativeLayout>
Adapter 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:background="#color/darkness">
<RelativeLayout
android:id="#+id/rlLeftLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/skypeGray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgLeftScoreBackground"
android:layout_alignTop="#+id/imgLeftPoster"
android:layout_toEndOf="#+id/imgLeftScoreBackground"
android:layout_toRightOf="#+id/imgLeftScoreBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/tvLeftTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:text="Captain America: Civil War"
android:textAlignment="viewEnd"
android:textColor="#color/redmilk"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvLeftOverview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:lineSpacingExtra="5dp"
android:maxLines="5"
android:text="Following the events of Age of Ultron, the collective goverments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers, causing two factions to side..."
android:textAlignment="viewEnd"
android:textColor="#color/colorWhite"
android:textSize="12sp"/>
<TextView
android:id="#+id/tvLeftCast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Chris Evans, Robert Downey Jr."
android:textAlignment="viewEnd"
android:textColor="#color/colorWhite"
android:textSize="12sp"
android:textStyle="bold|italic"/>
</LinearLayout>
<ImageView
android:id="#+id/imgLeftPoster"
android:layout_width="120dp"
android:layout_height="180dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="#drawable/poster"/>
<ImageView
android:id="#+id/imgLeftScoreBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imgLeftPoster"
android:layout_marginTop="3dp"
app:srcCompat="#drawable/background"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlRightLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/skypeGray">
<ImageView
android:id="#+id/imgRightPoster"
android:layout_width="120dp"
android:layout_height="180dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
app:srcCompat="#drawable/poster"/>
<ImageView
android:id="#+id/imgRightScoreBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imgRightPoster"
android:layout_marginTop="3dp"
app:srcCompat="#drawable/background_right"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgRightScoreBackground"
android:layout_alignTop="#+id/imgRightPoster"
android:layout_toLeftOf="#+id/imgRightScoreBackground"
android:layout_toStartOf="#+id/imgRightScoreBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/tvRightTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:text="Captain America: Civil War"
android:textAlignment="viewStart"
android:textColor="#color/redmilk"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvRightOverview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:lineSpacingExtra="5dp"
android:maxLines="5"
android:text="Following the events of Age of Ultron, the collective goverments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers, causing two factions to side..."
android:textAlignment="viewStart"
android:textColor="#color/colorWhite"
android:textSize="12sp"/>
<TextView
android:id="#+id/tvRightCast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Chris Evans, Robert Downey Jr."
android:textAlignment="viewStart"
android:textColor="#color/colorWhite"
android:textSize="12sp"
android:textStyle="bold|italic"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
You are getting a wrong id:
Change these lines:
#BindView(android.R.id.list)
ListView mListView;
to :
#BindView(R.id.ListView)
ListView mListView;
You are using listview id which is inbuild android.R.id.list which requires you to extend your activity with ListActivity.
So extend your activity with ListActivity. like this
public class MainActivity extends ListActivity
Or, If you use use custom listview with your id, then use same activity, just give proper id which you defined in your xml file and initialize it.
Something like this
ListView list = (ListView) view.findViewById(R.id.yourlistviewid);

List.setAdapter(adapter) in FragmentActivity throwing an error with spinners

Any help is appreciated. I am still learning Android and have looked through a lot of questions but can't seem to find the solution. Again, thanks for helping!
I am getting an error where I cannot get properly set up my ArrayAdapter in a FragmentActivity in the setupeverything method. I have spinners providing years and terms, but I do not know why list.setAdapter is throwing an error...
Here is the code I have so far...
public class MainActivity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
variables here...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setupeverything(); //LINE 49
}
private void setUpView(){
_mViewPager= (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(), getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
}
public static void refreshview()
{
int year = spinner_year.getSelectedItemPosition();
int term = spinner_term.getSelectedItemPosition();
listofclasses.clear();
for(int i=0;i<terms[year][term].getSize();i++)
{
listofclasses.add(terms[year][term].getCourse(i).getname() + " " + terms[year][term].getCourse(i).getcredits()+ " " + terms[year][term].getCourse(i).getgrade());
}
adapterthing.notifyDataSetChanged();
setcreditandgpa();
}
public void buttonaddclass(View view)
{
int year = spinner_year.getSelectedItemPosition();
int term = spinner_term.getSelectedItemPosition();
terms[year][term].addClass("test"+count, "B+", 4);
refreshview();
//Intent openaddclass = new Intent("AddClassActivity");
//startActivity(openaddclass); //Removed because changed to fragments
}
public void setupeverything()
{
ListView list = (ListView) findViewById(R.id.listview_classes);
adapterthing = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, listofclasses);
list.setAdapter(adapterthing); //LINE 245
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
deleteclass(position);
return true;
}});
array_year=new String[5];
array_year[0]="1";
array_year[1]="2";
array_year[2]="3";
array_year[3]="4";
array_year[4]="5";
spinner_year = (Spinner) findViewById(R.id.spinner_year);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_year);
spinner_year.setAdapter(adapter);
array_term=new String[4];
array_term[0]="F";
array_term[1]="W";
array_term[2]="S";
array_term[3]="S";
spinner_term = (Spinner) findViewById(R.id.spinner_term);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_term);
spinner_term.setAdapter(adapter1);
for (int i = 0; i<5; i++)
{
for(int j = 0; j<4;j++)
{
terms[i][j] = new ClassList();
}
}
spinner_year.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
refreshview();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner_term.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
refreshview();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
edittotalcredit = (EditText) findViewById(R.id.edittext_credits);
edittermcredit = (EditText) findViewById(R.id.edittext_credits_term);
edittotalgpa = (EditText) findViewById(R.id.edittext_gpa);
edittermgpa = (EditText) findViewById(R.id.edittext_gpa_term);
}
LogCat:
05-29 00:40:31.415 2713-2713/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.drexel.drexelgpacalc/com.drexel.drexelgpacalc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
at com.drexel.drexelgpacalc.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
XML Layouts:
activity_main.xml is the first fragment to be shown to display data from chosen course lists from addclasslayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Spinner
android:id="#+id/spinner_year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Spinner
android:id="#+id/spinner_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:id="#+id/listview_classes"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_weight="4" >
</ListView>
<Button
android:id="#+id/button_submit"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Add Class"
android:onClick="buttonaddclass" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_credits_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Credits This Term:"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext_credits_term"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
<TextView
android:id="#+id/textview_gpa_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="GPA This Term:"
android:paddingLeft="20dp"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext_gpa_term"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_credits"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Credits Overall:"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext_credits"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
<TextView
android:id="#+id/textview_gpa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="GPA Overall:"
android:paddingLeft="20dp"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext_gpa"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
</LinearLayout>
</LinearLayout>
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/viewPager" />
</LinearLayout>
AddClassLayout.xml Second Fragment which pulls data from a list and supposed to send into listofclasses.
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonclick"
android:text="Add Class" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_weight="3.5"
android:layout_height="wrap_content"
android:hint="Start Entering Class Here"
android:ems="10"
android:singleLine="true"/>
<Spinner
android:id="#+id/expandableListView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:hint="Grade"
android:entries="#array/gradeArray" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
As pointed out in the comments.
In the onCreate you have setContentView(R.layout.main) but main.xml doesn't have a listview defined in it with the id "listview_classes".
You have a view pager but I don't see anywhere where you use R.layout.activity_main. You need to be using a layoutinflater to setup R.layout.activity_main and set it on the view pager or just ditch the whole idea of a view pager.
It looks like it would be simpler to just setContentView(R.layout.activity_main) and remove the code about the view pager unless you have other reasons for wanting it there.
this is because your listOfClasses that you pass to an array adapter is null.
you got to fill this list before you are passing it there... try this solution...
Simply you NullPointerException
Caused by: java.lang.NullPointerException
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
at com.drexel.drexelgpacalc.MainActivity.onCreate(MainActivity.java:49)
just click twice the line
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
and eclipse will show you where the trouble is.
and you'll got to recognize what variable has null value..
think it's a list_of_classes.
LogCat is a powerfull toll for developers ;)
I think you are using listofclasses without instantiation. Thus getting NullPointerException.
BTW
Are you calling refreshview() method to populate your vector?

Categories

Resources