【问题标题】:Outputting in different layout以不同的布局输出
【发布时间】:2015-10-08 15:54:12
【问题描述】:

我目前正在开发一个移动杂货应用程序。我只是想问你为什么我在布局 Y 中的代码不能从不同的活动中抛出细节,但布局 X 可以!如您所见,我使用复选框选中了您要购买的所有商品,但在布局 Y 中,如果您选中了您想要的商品并单击“添加到购物车”按钮,它就会崩溃。 我根据各自的信息重新编辑代码并复制到布局 Y。

这是我的代码

婴儿尿布(布局 X)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 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_Diaper.this, Shopping_List.class);
    x.putExtra("items", final_text.getText().toString());

    startActivity(x);
}

婴儿食品(布局 Y)Java

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addtoFood = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_food);
    setContentView(R.layout.activity_baby__food);
}
public void SelectItem (View view) {

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

            if(checked)
            {selection.add("Cory & Gate");}
            else
            {
                selection.remove ("Cory & Gate");
            }

            break;

        case R.id.gerber:

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

            break;

        case R.id.hipp:

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

            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_Food.this, Shopping_List.class);
    x.putExtra("items", final_text.getText().toString());

    startActivity(x);
}

婴儿尿布(布局 X)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="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_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="ocgtshoppinglist"
    />

婴儿食品(布局 Y)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_Food"
android:id="@+id/baby_food">


<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cory &amp; Gate"
    android:id="@+id/coryandgate"
    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="Gerber"
    android:id="@+id/gerber"
    android:layout_below="@+id/coryandgate"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"

    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hipp"
    android:id="@+id/hipp"
    android:layout_below="@+id/gerber"
    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_food"
    android:layout_below="@+id/hipp"
    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="ocgtshoppinglist" />

当我点击 Logcat 上的链接时,它指向我 final_text.setText(final_shopping_selection);布局Y

【问题讨论】:

  • 不太确定问题是什么或被问到什么。我真的不知道这是什么意思:why my code in layout Y cant throw the details from different你能编辑你的问题来澄清一下吗?
  • @tnw 现在再次阅读先生。明白了吗?
  • 仍然不够详细。 it crashed 那么错误是什么?
  • @tnw 每次我单击添加到购物车按钮时,应用程序都会崩溃。
  • 那是因为发生了错误/异常。你需要找出它是什么。

标签: java android android-layout android-intent layout


【解决方案1】:

尝试在 Y 中更改它

    addtoFood = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_food);
    setContentView(R.layout.activity_baby__food);

    setContentView(R.layout.activity_baby__food);
    addtoFood = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_food);

我认为您的文本视图在 addtocart 方法中为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    相关资源
    最近更新 更多