4

Can't start new activity. Got problem in onClickListener.

I post a code an errors. Have not any idea why I got error in this line.

I think it can be not initialized variable Button b, but it's initialized!

Help, plz.

Code:

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();
            }
        });
    }
}

Error:

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)

Manifest:

    <activity
        android:name=".newItem"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="stateAlwaysVisible" >
    </activity>

Err here:

b.setOnClickListener(new OnClickListener() {
Val
  • 4,225
  • 8
  • 36
  • 55

3 Answers3

3

Try to use

implements View.OnClickListener

or try to use Listener as anonymous class and then tell me if it works.


Note: class names should start with uppercase and you should respect it.


Update:

So problem is elsewhere, make sure that your ID is correct! and belongs to Button.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Activity is added in Manifest.xml. What u think about problem with startActivity() ? – Val Jul 08 '12 at 14:21
  • Try to remove `this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);` – Simon Dorociak Jul 08 '12 at 14:27
  • you don't understand, I can't create activity of this class, because I got err in this line. I can't click the button because my app crashing before I'll see the button. – Val Jul 08 '12 at 14:31
  • see my comment above, try to remove line that i meant. – Simon Dorociak Jul 08 '12 at 14:32
3

You can try like this:

     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();
                    ...
             }
       });

Also make sure you have the right Button definition in your XML file

     <Button 
     android:layout_height="wrap_content"
     android:id="@+id/but_Ok" 
     android:text="Your Button Text"
     android:layout_width="fill_parent"
     />
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
  • post your entire code with the line where the `Null pointer Exception` occurs and that will help solve your problem quickly – Parth Doshi Jul 08 '12 at 14:20
  • arror in this line : b.setOnClickListener(this); what more code do u need? – Val Jul 08 '12 at 14:23
-1

You need to set up the button variable above the onCreate(Bundle) I have also changed the way the onclick activity is set try this

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();
    }
});


    }
}
user1078385
  • 344
  • 2
  • 6
  • 21