将Spinner / EditText中的值从一个活动传递到另一个活动

时间:2013-04-24 18:49:03

标签: android android-intent android-edittext android-spinner

当我将值直接从字符串传递给另一个活动时,它可以正常工作。但是,如果我从EditText和Spinner中检索相同的值,然后单击按钮(启动新活动),则应用程序崩溃。

有了这个我的应用程序工作:

    String key;
    String para;
    Button btnViewProducts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        //StrictMode.enableDefaults();

        // Buttons
        btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
        key = "kq";
        para = "Taxi_No";

        // View products click event
        btnViewProducts.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // Launching All products Activity
                Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);

                i.putExtra("keyval", key);
                i.putExtra("paraval",para);
                startActivity(i);
            }
        });

        // View products click event
    }
}

但这不起作用:

    String key;
    String para;
    Spinner spin;
    EditText keyw;
    Button btnViewProducts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        //StrictMode.enableDefaults();

        btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
        Spinner spin = (Spinner)findViewById(R.id.spinpara);
        EditText keyw = (EditText)findViewById(R.id.kser);
        key = keyw.getText().toString();
        para = spin.getSelectedItem().toString();

        // View products click event
        btnViewProducts.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // Launching All products Activity
                Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
                i.putExtras(b);
                i.putExtra("keyval", key);
                 i.putExtra("paraval",para);
                startActivity(i);
            }
        });

        // View products click event
    }
}

这是我的LogCat输出:

04-25 00:58:31.634: W/ResourceType(335): Skipping entry 0x7f040003 in package table 0 because it is not complex!
04-25 00:58:31.644: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-25 00:58:31.695: E/AndroidRuntime(335): FATAL EXCEPTION: main
04-25 00:58:31.695: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.MainScreenActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class android.widget.Spinner
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.os.Looper.loop(Looper.java:123)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-25 00:58:31.695: E/AndroidRuntime(335):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 00:58:31.695: E/AndroidRuntime(335):     at java.lang.reflect.Method.invoke(Method.java:507)
04-25 00:58:31.695: E/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-25 00:58:31.695: E/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-25 00:58:31.695: E/AndroidRuntime(335):     at dalvik.system.NativeStart.main(Native Method)
04-25 00:58:31.695: E/AndroidRuntime(335): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class android.widget.Spinner
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
04-25 00:58:31.695: E/AndroidRuntime(335):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-25 00:58:31.695: E/AndroidRuntime(335):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.Activity.setContentView(Activity.java:1657)
04-25 00:58:31.695: E/AndroidRuntime(335):     at com.example.androidhive.MainScreenActivity.onCreate(MainScreenActivity.java:30)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-25 00:58:31.695: E/AndroidRuntime(335):     ... 11 more
04-25 00:58:31.695: E/AndroidRuntime(335): Caused by: java.lang.reflect.InvocationTargetException
04-25 00:58:31.695: E/AndroidRuntime(335):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-25 00:58:31.695: E/AndroidRuntime(335):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
04-25 00:58:31.695: E/AndroidRuntime(335):     ... 22 more
04-25 00:58:31.695: E/AndroidRuntime(335): Caused by: java.lang.NullPointerException
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.content.res.Resources.getTextArray(Resources.java:361)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.widget.AbsSpinner.<init>(AbsSpinner.java:73)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.widget.Spinner.<init>(Spinner.java:56)
04-25 00:58:31.695: E/AndroidRuntime(335):     at android.widget.Spinner.<init>(Spinner.java:52)
04-25 00:58:31.695: E/AndroidRuntime(335):     ... 25 more

我必须从文本字段和微调器选择的值中获​​取这些参数。必须对代码进行哪些更改?

1 个答案:

答案 0 :(得分:1)

阅读logcat,这一行告诉我们这个问题

    Caused by: java.lang.NullPointerException
    04-25 00:58:31.695: E/AndroidRuntime(335):  at
    android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)

告诉我们您Array使用的Spinnernull。检查您是否在Array

中使用了Spinner的正确名称

另请注意,通常情况下使用Activity Context代替Application Context通常会更好,除非您真正需要它。您的Intent可能是

Intent i = new Intent(ActivityName.this, AllProductsActivity.class);

这显然不是你的问题,但需要牢记

<强> logcat的

Logcat起初可能很棘手,但是找一行Caused by:NullpointerException, IllegalArgument, etc...,然后第一行代码引用你的项目通常是一个好的起点