無法啓動新活動。在onClickListener中遇到問題。無法啓動活動,setOnClickListener
我發表了一條錯誤代碼。不知道爲什麼我在這一行中出現錯誤。
我認爲它可以不初始化變量按鈕b,但它已初始化!
幫助,PLZ。
代碼:
public class newItem extends Activity {
private EditText Quantity;
private EditText Weight;
private EditText Price;
private EditText Title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_list);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Button b = (Button) findViewById(R.id.ok_but);
Quantity = (EditText) findViewById(R.id.Quantity);
Weight = (EditText) findViewById(R.id.Weight);
Price = (EditText) findViewById(R.id.Price);
Title = (EditText) findViewById(R.id.Title);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("Quantity", Quantity.getText().toString());
i.putExtra("Weight", Weight.getText().toString());
i.putExtra("Price", Price.getText().toString());
i.putExtra("Title", Title.getText().toString());
setResult(RESULT_OK, i);
Log.i("palval", "setResult(RESULT_OK, i); newItem");
finish();
}
});
}
}
錯誤:
07-08 17:26:42.402: E/AndroidRuntime(31626): FATAL EXCEPTION: main
07-08 17:26:42.402: E/AndroidRuntime(31626): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ashopping_list/com.example.ashopping_list.newItem}: java.lang.NullPointerException
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.os.Looper.loop(Looper.java:154)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread.main(ActivityThread.java:4945)
07-08 17:26:42.402: E/AndroidRuntime(31626): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 17:26:42.402: E/AndroidRuntime(31626): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 17:26:42.402: E/AndroidRuntime(31626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-08 17:26:42.402: E/AndroidRuntime(31626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-08 17:26:42.402: E/AndroidRuntime(31626): at dalvik.system.NativeStart.main(Native Method)
07-08 17:26:42.402: E/AndroidRuntime(31626): Caused by: java.lang.NullPointerException
07-08 17:26:42.402: E/AndroidRuntime(31626): at com.example.ashopping_list.newItem.onCreate(newItem.java:32)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.Activity.performCreate(Activity.java:4531)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-08 17:26:42.402: E/AndroidRuntime(31626): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
清單:
<activity
android:name=".newItem"
android:label="@string/title_activity_main"
android:windowSoftInputMode="stateAlwaysVisible" >
</activity>
錯誤的位置:
b.setOnClickListener(new OnClickListener() {
這是26行newItem.java:26 – 2012-07-08 14:11:29
你可以plz發佈整個代碼而不是「...」 – jaisonDavis 2012-07-08 14:16:41