Outputting from one layout to another

I have 2 layouts named Layout X(Baby_diaper) and layout Y(Baby_milk). In layout X, I worked to output the details from Layout X to Shopping List layout(This layout is the overall receiver of all items) when I click the “Add to cart” button. Now, when I used the same codes in Layout Y, I mean reedit according to the details of layout Y, it aint works.

Here’s the code to be exact.

Baby_Diaper (LayoutX) JAVA

public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
Intent i = new Intent(this, Shopping_List.class);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_baby__diaper);
    addtoCart = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_diaper);

}


public void SelectItem (View view) {

    boolean checked = ((CheckBox) view) .isChecked();
    switch (view.getId())
    {
        case R.id.pampers:

            if(checked)
            {selection.add("Pampers");}
            else
            {
                selection.remove ("Pampers");
            }

            break;

        case R.id.huggies:

            if(checked)
            {selection.add("Huggies");}
            else
            {
                selection.remove ("Huggies");
            }

            break;

        case R.id.johnsons:

            if(checked)
            {selection.add("Johnsons");}
            else
            {
                selection.remove ("Johnsons");
            }

            break;

        case R.id.supreme:

            if(checked)
            {selection.add("Supreme");}
            else
            {
                selection.remove ("Supreme");
            }

            break;
    }
}

public void diapertocart(View view){
    String final_shopping_selection = "";

    for (String Selections  : selection){
        final_shopping_selection = final_shopping_selection + Selections + "n";
    }

    final_text.setText(final_shopping_selection);
    final_text.setEnabled(true);}

public void diapertoshoppinglist (View view){
    Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
    x.putExtra("items", final_text.getText().toString());

    startActivity(x);
}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_baby__diaper, menu);
    return true;
}

Baby_Milk (LayoutY) JAVA

public class Baby_Milk extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoMilk;
Intent i = new Intent(this, Shopping_List.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_baby__milk);
    addtoMilk = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_diaper);

}
public void SelectItem (View view) {

    boolean checked = ((CheckBox) view) .isChecked();
    switch (view.getId())
    {
        case R.id.aptamil:

            if(checked)
            {selection.add("Aptamil");}
            else
            {
                selection.remove ("Aptamil");
            }

            break;

        case R.id.bonna:

            if(checked)
            {selection.add("Bonna");}
            else
            {
                selection.remove ("Bonna");
            }

            break;

        case R.id.johnsons:

            if(checked)
            {selection.add("Johnsons");}
            else
            {
                selection.remove ("Johnsons");
            }

            break;

        case R.id.supreme:

            if(checked)
            {selection.add("Supreme");}
            else
            {
                selection.remove ("Supreme");
            }

            break;
    }
}

public void ocaddtocart(View view){
    String final_shopping_selection = "";

    for (String Selections  : selection){
        final_shopping_selection = final_shopping_selection + Selections + "n";
    }

    final_text.setText(final_shopping_selection);
    final_text.setEnabled(true);}

public void ocgtshoppinglist (View view){
    Intent x = new Intent(Baby_Milk.this, Shopping_List.class);
    x.putExtra("items", final_text.getText().toString());

    startActivity(x);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_baby__milk, menu);
    return true;
}

Baby_Diaper 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="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="@+id/baby_diaper">


<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pampers"
    android:id="@+id/pampers"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="52dp"
    android:checked="false"
    android:onClick="SelectItem"
    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/huggies"
    android:id="@+id/huggies"
    android:layout_below="@+id/pampers"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"

    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/johnsons"
    android:id="@+id/johnsons"
    android:layout_below="@+id/huggies"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"
    android:inputType="textNoSuggestions"

    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/supreme"
    android:id="@+id/supreme"
    android:layout_below="@+id/johnsons"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"
    android:inputType="textNoSuggestions"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/addtocart"
    android:id="@+id/addtocart"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:inputType="textNoSuggestions"
    android:onClick="diapertocart" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Hello Shoppers!"
    android:id="@+id/final_shopping_diaper"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="80dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO TO SHOPPING LIST"
    android:id="@+id/gt_shopping_list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="diapertoshoppinglist"
    />

Baby_Milk 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="com.example.admin.mobile_grocery.Baby_Milk"
android:id="@+id/baby_milk">

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Aptamil"
    android:id="@+id/aptamil"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="52dp"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bonna"
    android:id="@+id/bonna"
    android:layout_below="@+id/aptamil"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enfamil"
    android:id="@+id/Enfamil"
    android:layout_below="@+id/bonna"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lactum"
    android:id="@+id/lactum"
    android:layout_below="@+id/Enfamil"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nan"
    android:id="@+id/nan"
    android:layout_below="@+id/lactum"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ADD TO CART / REMOVE"
    android:id="@+id/addtocart"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="ocaddtocart" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Hello Shoppers!"
    android:id="@+id/final_shopping_milk"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO TO SHOPPING LIST"
    android:id="@+id/gt_shopping_list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="ocgtshoppinglist" />

Logcat Error

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3724)
            at android.view.View.performClick(View.java:4261)
            at android.view.View$PerformClick.run(View.java:17356)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3719)
            at android.view.View.performClick(View.java:4261)
            at android.view.View$PerformClick.run(View.java:17356)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.admin.mobile_grocery.Baby_Milk.ocaddtocart(Baby_Milk.java:88)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3719)
            at android.view.View.performClick(View.java:4261)
            at android.view.View$PerformClick.run(View.java:17356)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

When I clicked the (Baby_Milk.java:88) it pointed me to
final_text.setText(final_shopping_selection);


Source: xml

Leave a Reply