-1

i have one button n 6 radio buttons in activity. after getting input from edit text on click of button(search) it goes to another activity. below is my code

public void search(View v)
{
    switch(v.getId())
    {               
    case R.id.button1:
        EditText et1= (EditText)findViewById(R.id.editText1);
        RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
        RadioButton r2 = (RadioButton) findViewById(R.id.radio1);
        RadioButton r3 = (RadioButton) findViewById(R.id.radio2);
        RadioButton r4 = (RadioButton) findViewById(R.id.radio3);
        RadioButton r5 = (RadioButton) findViewById(R.id.radio4);
        RadioButton r6 = (RadioButton) findViewById(R.id.radio5);
        pin=et1.getText().toString();
        if(pin.length()==0){
            Toast.makeText(this, "Please provide Input!", Toast.LENGTH_SHORT).show();
            return;
        }
        if(r1.isChecked())
        {   category="All";
        Intent i=new Intent(this,Details.class);
        startActivity(i);
        }


        else if(r2.isChecked())
        {   
            category="Food";
            Intent i=new Intent(this,Details.class);
            startActivity(i);
        }

        else if(r3.isChecked())
        {   
            category="Entertainment";
            Intent i=new Intent(this,Details.class);
            startActivity(i);
        }

        else if(r4.isChecked())
        {   
            category="Shopping";
            Intent i=new Intent(this,Details.class);
            startActivity(i);
        }

        else if(r5.isChecked())
        {   
            category="Health";
            Intent i=new Intent(this,Details.class);
            startActivity(i);
        }

        else if (r6.isChecked())
        {   
            category="Services";
            Intent i=new Intent(this,Details.class);
            startActivity(i);

        }
         break;                     
    }
/*if(pin.length()!=0&&category.length()!=0)
 {
Intent i=new Intent(this,Details.class);
startActivity(i);

}*/

}

is the above code correct?? while running force close happens when input given .but when no input the toast msg is displayed.

the log cat is

09-29 23:56:25.706: E/AndroidRuntime(23196): FATAL EXCEPTION: main
09-29 23:56:25.706: E/AndroidRuntime(23196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yellowpages/com.yellowpages.Details}: java.lang.NullPointerException
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.os.Looper.loop(Looper.java:137)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at java.lang.reflect.Method.invoke(Method.java:511)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at dalvik.system.NativeStart.main(Native Method)
09-29 23:56:25.706: E/AndroidRuntime(23196): Caused by: java.lang.NullPointerException
09-29 23:56:25.706: E/AndroidRuntime(23196):    at com.yellowpages.Dbpages.fetchRecord(Dbpages.java:109)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at com.yellowpages.Details.onCreate(Details.java:22)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.Activity.performCreate(Activity.java:4465)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-29 23:56:25.706: E/AndroidRuntime(23196):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-29 23:56:25.706: E/AndroidRuntime(23196):    ... 11 more




package com.yellowpages;

   import android.annotation.SuppressLint;
   import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class UserSearch extends Activity {
    public static String category="",pin="";
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_search);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_user_search, menu);
        return true;
    }
    public void search(View v)
    {
        switch(v.getId())
        {               
        case R.id.button1:
            EditText et1= (EditText)findViewById(R.id.editText1);
            RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
            RadioButton r2 = (RadioButton) findViewById(R.id.radio1);
            RadioButton r3 = (RadioButton) findViewById(R.id.radio2);
            RadioButton r4 = (RadioButton) findViewById(R.id.radio3);
            RadioButton r5 = (RadioButton) findViewById(R.id.radio4);
            RadioButton r6 = (RadioButton) findViewById(R.id.radio5);
            pin=et1.getText().toString();
            if(pin.length()==0){
                Toast.makeText(this, "Please provide Input!", Toast.LENGTH_SHORT).show();
                return;
            }
            if(r1.isChecked())
            {   category="All";
            Intent i=new Intent(this,Details.class);
            startActivity(i);
            }


            else if(r2.isChecked())
            {   
                category="Food";
                Intent i=new Intent(this,Details.class);
                startActivity(i);
            }

            else if(r3.isChecked())
            {   
                category="Entertainment";
                Intent i=new Intent(this,Details.class);
                startActivity(i);
            }

            else if(r4.isChecked())
            {   
                category="Shopping";
                Intent i=new Intent(this,Details.class);
                startActivity(i);
            }

            else if(r5.isChecked())
            {   
                category="Health";
                Intent i=new Intent(this,Details.class);
                startActivity(i);
            }

            else if (r6.isChecked())
            {   
                category="Services";
                Intent i=new Intent(this,Details.class);
                startActivity(i);

            }
             break;                     
        }
/*if(pin.length()!=0&&category.length()!=0)
{
    Intent i=new Intent(this,Details.class);
    startActivity(i);

}*/

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
Chetan
  • 23
  • 7
  • 1
    what is category? You should show the line of the NullPointerException ( at com.yellowpages.Dbpages.fetchRecord(Dbpages.java:109) ). Probably category is not passed properly. You should pass parameters to a newly created activity via an intent. – Rémi Sep 29 '12 at 18:39
  • Are you getting Force close when you are clicking on radio button, is it?? – Vishesh Chandra Sep 29 '12 at 18:44
  • 1
    see this question to properly pass a string from one activity to another: http://stackoverflow.com/questions/6707900/how-to-pass-string-one-activity-to-another – Rémi Sep 29 '12 at 18:47
  • @visheshchandra no i get force close when i press button after giving input in edit text and selecting radio button – Chetan Sep 29 '12 at 18:47
  • @Rémi : i am not passing any values just assigning the values of edit text to a string and radio buttons value to category. both are string and public variable so that in next i can access using Classname.variablename – Chetan Sep 29 '12 at 18:49
  • As Rémi said, the error is on line 109 in Dbpages.java. Could you post that code and indicate which line is 109? – Sam Sep 29 '12 at 18:51
  • Cursor c=mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_PHONE,KEY_CATEGORY,KEY_PINCODE},"KEY_PINCODE = ? AND KEY_CATEGORY =?",new String[]{pin,categ}, null, null, KEY_NAME); – Chetan Sep 29 '12 at 19:43
  • @Sam above is the 109 line in Dbpages.java – Chetan Sep 30 '12 at 06:12

3 Answers3

0

Using static variables to pass parameters to an activity is a bug. When your app is in the background, it may be killed by the system in order to recover memory. If the app is then moved back to the front, your activity will be restored, but the global variables won't be there any more.

See this question to properly pass a string from one activity to another: Pass a String from one Activity to another Activity in Android

You should do something like:

Intent i = new Intent(this, Details.class);
i.putExtra("category", "Food");
startActivity(i); 

and in Details.class:

String category = intent.getExtras().getString("category");

Because you did not post your code, it is not very clear where the raised exception comes from. Are you sure the "categ" variable is properly initialized to Details.category? Anyway, you should use intent extras.

Community
  • 1
  • 1
Rémi
  • 3,705
  • 1
  • 28
  • 39
  • never use hard coded strings like "category" in android. If you want to change this key-string for some reason, you have to change it at every point in your program, where you use it. Use for example a separated keys.xml file with string entries. – Jan Koester Sep 29 '12 at 22:38
  • You did not post the code of the Details activity, where the error occurs. – Rémi Sep 30 '12 at 09:55
0

Do one thing, you can declare the variable Globally and initialize from oncreate instead of onclick,

EditText et1= (EditText)findViewById(R.id.editText1);
    RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
    RadioButton r2 = (RadioButton) findViewById(R.id.radio1);
    RadioButton r3 = (RadioButton) findViewById(R.id.radio2);
    RadioButton r4 = (RadioButton) findViewById(R.id.radio3);
    RadioButton r5 = (RadioButton) findViewById(R.id.radio4);
    RadioButton r6 = (RadioButton) findViewById(R.id.radio5);

because i think when you are selecting the radio button that time Radio button is Null. or please post your whole Activity java class here for exact solution.

Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
0

These lines tell us that the problem is in Dbpages.java on line 109:

Caused by: java.lang.NullPointerException
    at com.yellowpages.Dbpages.fetchRecord(Dbpages.java:109)

A NullPointerException happens when a variable is null and you try to access one of its methods.
This is line 109:

Cursor c=mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_PHONE,KEY_CATEGORY,KEY_PINCODE},"KEY_PINCODE = ? AND KEY_CATEGORY =?",new String[]{pin,categ}, null, null, KEY_NAME);

You are only trying to access one variable mDb and use .query(). But mDb is null, you never initialized it. Hence a NullPointerException.

Answer
You need to initialize your database by using mDb = ... before trying to query it.

Sam
  • 86,580
  • 20
  • 181
  • 179