0

我正在写一本食谱书,我遇到了一个问题 - 我想将我的食谱列表中的文本发送到食谱显示屏幕,我一定是做错了什么,因为我一直在强制关闭:

这是我的 Recipe_Button_List 的代码

公共类 Recipe_Button_List 扩展 Activity {

EditText输入法;

EditText 输入成分;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recipe_button_list);

    inputMethod = (EditText) findViewById(R.id.textView2);
    inputIngredients = (EditText) findViewById(R.id.textView1);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);



    Button mainNext = (Button) findViewById(R.id.Recipe1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            final TextView mTextView = (TextView) findViewById(R.id.textView1);
            mTextView.setText(R.string.Test);
            Intent i= new Intent(getBaseContext(),recipedisplayscreen.class);
            //Sending data to the next screen
            i.putExtra("textView1", inputIngredients.getText().toString());
            i.putExtra("textView2", inputMethod.getText().toString());

            Log.e("n", inputMethod.getText()+"."+ inputIngredients.getText());

            startActivity(i);
        }
    });
}
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.recipe_menu1, menu);
     return true;

}

}

这是我的 recipe_display_screen:

公共类 recipedisplayscreen 扩展 Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recipedisplayscreen);

    TextView MethodDisplay = (TextView) findViewById(R.id.textView2);
    TextView IngredientsDisplay = (TextView) findViewById(R.id.textView4);

    Intent i = getIntent();
    String Ingredients = i.getStringExtra("TextView1");
    String Method = i.getStringExtra("TextView2");
    Log.e("recipedisplayscreen", Ingredients + "." + Method);

    MethodDisplay.setText(Method);
    IngredientsDisplay.setText(Ingredients);


    ActionBar actionBar = getActionBar();
    setTitle(R.string.title);
    actionBar.setDisplayHomeAsUpEnabled(true);}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // App icon in action bar clicked; go home
                Intent intent = new Intent(this, MainScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }




 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.recipe_menu1, menu);
        return true;

}

}

以下是来自 logCat 的记录:

05-01 21:54:54.638: D/AndroidRuntime(10717): 关闭 VM 05-01 21:54:54.638: W/dalvikvm(10717): threadid=1: 线程退出未捕获异常 (group=0x40a301f8) 05 -01 21:54:54.638: E/AndroidRuntime(10717): 致命异常: main 05-01 21:54:54.638: E/AndroidRuntime(10717): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.bluStudios .Recipes4U.ics/com.bluStudios.Recipes4U.ics.Recipe_Button_List}:java.lang.ClassCastException:android.widget.TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638:E/AndroidRuntime(10717 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05- 01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread。访问$600(ActivityThread.java:123) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 05-01 21:54:54.638 : E/AndroidRuntime(10717): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.os.Looper.loop(Looper. java:137) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.main(ActivityThread.java:4424) 05-01 21:54:54.638: E/AndroidRuntime(10717) : 在 java.lang.reflect.Method.invokeNative(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 java.lang.reflect.Method.invoke(Method.java:511) 05- 01 21:54:54.638: E/AndroidRuntime(10717): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-01 21:54:54.638: E/AndroidRuntime(10717) : 在 com.android。internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-01 21:54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638 : E/AndroidRuntime(10717): 引起: java.lang.ClassCastException: android.widget.TextView 不能转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com. bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread .performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多os.ZygoteInit.main(ZygoteInit.java:551) 05-01 21:54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E /AndroidRuntime(10717): 引起: java.lang.ClassCastException: android.widget.TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios。 Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21: 54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多os.ZygoteInit.main(ZygoteInit.java:551) 05-01 21:54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E /AndroidRuntime(10717): 引起: java.lang.ClassCastException: android.widget.TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios。 Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21: 54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多main(ZygoteInit.java:551) 05-01 21:54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717) ): 引起: java.lang.ClassCastException: android.widget.TextView 不能转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics。 Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多main(ZygoteInit.java:551) 05-01 21:54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717) ): 引起: java.lang.ClassCastException: android.widget.TextView 不能转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics。 Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717): Caused by: java.lang.ClassCastException: android. widget.TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation .callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E /AndroidRuntime(10717): ... 11 更多54:54.638: E/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717): Caused by: java.lang.ClassCastException: android. widget.TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation .callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E /AndroidRuntime(10717): ... 11 更多NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 05-01 21 :54:54.638: E/AndroidRuntime(10717): 在 com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android。 app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54: 54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 还有 11 个NativeStart.main(Native Method) 05-01 21:54:54.638: E/AndroidRuntime(10717): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 05-01 21 :54:54.638: E/AndroidRuntime(10717): 在 com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android。 app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54: 54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 还有 11 个TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21: 54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime (10717): ... 11 更多TextView 无法转换为 android.widget.EditText 05-01 21:54:54.638: E/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21: 54:54.638: E/AndroidRuntime(10717): 在 android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime (10717): ... 11 更多Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E/AndroidRuntime(10717): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E/AndroidRuntime(10717): ... 11 更多

PS 当我按下将您带到 recipe_button_list 的按钮时,会发生强制关闭

编辑: 再次嗨 :) 我还有另一个小问题 - 我的 recipe_display_screen 从我以前的屏幕收到 null.null 知道为什么吗?

日志猫:

05-02 11:28:29.764: D/dalvikvm(28455): GC_CONCURRENT 释放 113K, 2% 空闲 14526K/14727K, 暂停 2ms+14ms 05-02 11:28:30.022: E/n(28455): FromStringMethod.if这显示然后 Intent 活动正常工作 05-02 11:28:30.163: D/dalvikvm(28455): GC_FOR_ALLOC freed 1769K, 13% free 12813K/14727K, paused 23ms 05-02 11:28:30.163: I/dalvikvm -heap(28455):将堆(碎片情况)增加到 13.883MB,分配 1401676 字节 05-02 11:28:30.202:D/dalvikvm(28455):GC_CONCURRENT 释放 3K,4% 空闲 14179K/14727K,暂停 2ms+ 2ms 05-02 11:28:30.218: E/recipedisplayscreen(28455): null.null

编辑结束

4

2 回答 2

2

啊! 您正在将文本视图投射到编辑框。看线

inputMethod = (EditText) findViewById(R.id.textView2);
inputIngredients = (EditText) findViewById(R.id.textView1);

将它们更改为

inputMethod = (TextView) findViewById(R.id.textView2);
inputIngredients = (TextView) findViewById(R.id.textView1);

并更改 inputMethod 和 inputIngredients 的声明。

除非你的意思是它们是 EditTexts,在这种情况下你需要在你的布局 recipe_button_list 中改变它

于 2012-05-01T21:04:51.733 回答
1

编辑: 在您的 Logcat 中,您将 TextView 转换为 EditText。将您的 XML 布局文件中的视图设置为 EditText 并且它必须工作:)

inputMethod = (EditText) findViewById(R.id.textView2);
inputIngredients = (EditText) findViewById(R.id.textView1);

编辑结束

你把你的数据放在你的第一个活动中:

Intent i= new Intent(this, recipedisplayscreen.class);
//Sending data to the next screen
i.putExtra("textView1", inputIngredients.getText().toString());
i.putExtra("textView2", inputMethod.getText().toString());

在 RecipeDisplayScreen 你可以在你的onCreate()

Bundle extras = getIntent().getExtras();

String myText1;
String myText2;


if(extras != null) {
   if (extras.containsKey("textView1") {
      myText1 = extras.getString("textView1");
   }
   if (extras.containsKey("textView2") {
      myText2 = extras.getString("textView2");
   }
}

我希望这个对你有用!如果没有,请发布您的日志输出:)

一些提示:考虑好的键名并将它们设置为常量:

public static final String INGREDIENTS_KEY = "INGREDIENTS_TEXT_KEY";
public static final String METHOD_KEY = "METHOD_TEXT_KEY";

用于类名 ALWAYS 大写 recipedisplayscreen=>RecipeDisplayScreen 对于字段使用小写字母: String Method => String txtMethod; TextView MethodDisplay=>TextView methodView;

于 2012-05-01T20:44:11.957 回答