文字列から別のアクティビティに値を直接渡すと、機能します。しかし、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
テキスト フィールドとスピナーで選択した値からこれらのパラメーターを取得する必要があります。コードにどのような変更を加える必要がありますか?