mercredi 30 novembre 2016

Getting a Null Object Reference when trying to set imageview resource with checkbox

So I have an app with a listview. When you click a listview item it opens a new activity. I placed a checkbox in this new activity where when its checked, it should put a checkmark next to the listview item on the previous screen. I'm running into this error I'm guessing because I'm trying to set the checkbox from the second activity that has a different content view than the listview. I hope I explained that well.

Heres my RouteDetails.java with the checkbox code

public class RouteDetails extends AppCompatActivity {

ImageView routeImage;
String routeName;
CheckBox routeCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_details);

    //back button for route details view
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    ///////checkbox///////////////////////////////////////////
    routeCheckBox = (CheckBox) findViewById(R.id.routeCheckBox);
    final ImageView checkImageView = (ImageView) findViewById(R.id.checkImageView);

    routeCheckBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
          if (routeCheckBox.isChecked())
          {
              checkImageView.setImageResource(R.drawable.birdsboroicon);
          }
        }
    });
    /////////////////////////////////////////////


    //sets actionbar title
    routeName = getIntent().getExtras().getString("routeName");
    getSupportActionBar().setTitle(routeName);

    //TextView for route details
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getCharSequence("route"));

    //ImageView for route details
    routeImage = (ImageView) findViewById(R.id.routeImage);
    final int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
    routeImage.setImageResource(mImageResource);

and heres the custom_row.xml that contains the imageview im trying to set based on the checkbox state.

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="51dp"
    android:id="@+id/routeText"
    android:layout_weight="1"
    android:textSize="18sp"
    android:gravity="center_vertical"
    android:textColor="@android:color/black"
    android:typeface="normal"
     />

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/checkImageView" />

So I want the checkmark to be next to the listview item after the back button is pressed.

Ill include this other code if it is relavent

heres my RouteDetails.xml that contains the checkbox

<ScrollView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.zach.listview.RouteDetails">


<CheckBox
    android:text="Route Climbed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/routeCheckBox"
    android:gravity="center" />

<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/routeDetailsView"
    android:textSize="18sp"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:layout_below="@id/routeCheckBox"/>


<ImageView
    android:layout_below="@id/routeDetailsView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/routeImage"
    android:scaleType="fitCenter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="true" />


</RelativeLayout>
</ScrollView>

My Custom adapter.java which creates each listview row

class CustomAdapter extends ArrayAdapter<CharSequence>{

    public CustomAdapter(Context context, CharSequence[] routes) {
        super(context, R.layout.custom_row ,routes);
    }

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater routeInflater = LayoutInflater.from(getContext());
    View customView = convertView;
    if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}

    CharSequence singleRoute = getItem(position);
    TextView routeText = (TextView) customView.findViewById(R.id.routeText);
    routeText.setText(singleRoute);

return customView;
}

my mainavtivity.java which is where the listview is populated

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Action Bar customization
    final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.actionbar);

    setContentView(R.layout.activity_main);

    ///// fill listview numbers I want to add
    final String[] routeListviewNumbers = getResources().getStringArray(R.array.routeNumbers);

    //fill list view with xml array of routes
    final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
    //fills route detail text view with xml array info
    final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail);
    //fills route detail image view with xml array of images
    final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);
    //image.recycle();

    //custom adapter for list view
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
    final ListView routeListView = (ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(routeAdapter);

    routeListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    CharSequence route = routeListViewItems[position];

                    int imageId = (int) image.getResourceId(position, -1);
                    if (route.equals(routeListViewItems[position]))
                    {
                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", routeDetail[position]);
                        intent.putExtra("imageResourceId", imageId);
                        intent.putExtra("routeName", routeListViewItems[position]);
                        startActivity(intent);
                    }
                }
            }
    );

}
}

and my activitymain.xml which is the listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="0dp"

tools:context="com.example.zach.listview.MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/routeListView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire