无法开始新活动。在onClickListener中遇到问题。
我发布了错误代码。我不知道为什么我在这一行得到错误。
我认为它可以不是初始化变量Button 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() {
答案 0 :(得分:3)
尝试使用
implements View.OnClickListener
或尝试使用Listener作为匿名类,然后告诉我它是否有效。
注意: 班级名称应以大写开头,您应该尊重它。
的更新强>
问题出在其他地方,确保你的ID
是正确的!并且属于Button
。
答案 1 :(得分:3)
您可以尝试这样:
Button b= (Button) findViewById(R.id.but_Ok);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(FromYourActivity.this,NextActivity.class);
startActivity(i);
finish();
...
}
});
还要确保XML文件中有正确的Button
定义
<Button
android:layout_height="wrap_content"
android:id="@+id/but_Ok"
android:text="Your Button Text"
android:layout_width="fill_parent"
/>
答案 2 :(得分:-1)
你需要在onCreate(Bundle)上面设置按钮变量我也改变了设置onclick活动的方式试试这个
public class newItem extends Activity {
private EditText Quantity;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_list);
Button b = (Button) findViewById(R.id.but_Ok);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
...
setResult(RESULT_OK, i);
finish();
}
});
}
}