2

I get data from my parse object,and the data is shown in the various views. However my app crashes whenever i click the showMenu button (onClick method is showMenu) This is my code

public class SingleRestraunt extends ActionBarActivity {
    private GoogleMap map;
    TextView resteName, resteCuisine, resteLocation, resteAddress, restePrice,
            resteTimings, restePayment, resteRating, resteWebsite;
    String restName, obj, restCuisine, restLocation, restAddress, restPrice,
            restTimings, restPayment, restRating, restWebsite;
    String[] menu = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_restraunt);
        resteName = (TextView) findViewById(R.id.restrauntName);
        resteCuisine = (TextView) findViewById(R.id.restrauntCuisine);
        resteLocation = (TextView) findViewById(R.id.restrauntLocation);
        resteAddress = (TextView) findViewById(R.id.restrauntAddress);
        restePrice = (TextView) findViewById(R.id.restrauntPrice);
        resteTimings = (TextView) findViewById(R.id.restrauntTimings);
        restePayment = (TextView) findViewById(R.id.restrauntPayment);
        resteRating = (TextView) findViewById(R.id.restrauntRating);
        resteWebsite = (TextView) findViewById(R.id.restrauntWebsite);

        Intent i = getIntent();
        obj = i.getStringExtra("restId");
        getDetails(obj);

    }

    private void getDetails(String obj) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
        query.getInBackground(obj, new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    restName = object.getString("name");
                    restCuisine = object.getString("cuisine");
                    restLocation = object.getString("locality");
                    restAddress = object.getString("address");
                    restPrice = object.getString("price");
                    restTimings = object.getString("timings");
                    restPayment = object.getString("accepted");
                    restRating = object.getString("ratings");
                    restWebsite = object.getString("URL");
                    JSONArray test = object.getJSONArray("menu");
                    for (int i = 0; i < test.length(); i++) {
                        try {
                            String menu1 = (String) test.get(i);
                            if (menu1 == null)
                                menu[i] = menu1;
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }
                    addData();

                } else {
                    e.printStackTrace();
                }
            }
        });

    }

    public void addData() {
        resteName.setText(restName);
        resteCuisine.setText(restCuisine);
        resteLocation.setText(restLocation);
        resteAddress.setText(restAddress);
        restePrice.setText(restPrice);
        resteTimings.setText(restTimings);
        restePayment.setText(restPayment);
        resteRating.setText(restRating);
        resteWebsite.setText(restWebsite);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.single_restraunt, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void Review(View v) {
        Intent i = new Intent(SingleRestraunt.this, ReviewPage.class);
        i.putExtra("RestName", restName);
        startActivity(i);
    }

    public void Offer(View v) {
        Intent i = new Intent(SingleRestraunt.this, OfferPage.class);
        i.putExtra("RestName", restName);
        startActivity(i);
    }

    public void showMenu(View v) {

        Bundle b = new Bundle();
        b.putStringArray("menuList", menu);
        Intent i = new Intent(SingleRestraunt.this, MenuActivity.class);
        i.putExtras(b);
        startActivity(i);
    }

    public void Share(View v) {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = ("Hey! I visited "
                + restName
                + " recommended to me by the Gastronoma App! Visit their site here " + restWebsite);
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }

}

The app seems to crash only when i click the showMenu button. Till that point the app works fine. All the other buttons do their intended tasks perfectly too. The error shown when the app crashes is

09-02 09:54:22.307: E/AndroidRuntime(3278): Process: com.example.gastronomaapp, PID: 3278
09-02 09:54:22.307: E/AndroidRuntime(3278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gastronomaapp/com.example.gastronomaapp.MenuActivity}: java.lang.NullPointerException

Open to any suggestions.EDIT as requested I have added both MenuActivity code and the Manifest file

public class MenuActivity extends Activity {
    WebView web = (WebView) findViewById(R.id.webView1);
    TextView next = (TextView) findViewById(R.id.next);
    TextView prev = (TextView) findViewById(R.id.prev);
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        Bundle b = this.getIntent().getExtras();
        String[] array = b.getStringArray("menuList");
        getMenu(array);
    }

    public void getMenu(final String[] array) {
        String menu = array[i];
        web.loadUrl(menu);
        visibility(i, array);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i++;
                String menu = array[i];
                web.loadUrl(menu);
                visibility(i, array);
            }

        });
        prev.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i--;
                String menu = array[i];
                web.loadUrl(menu);
                visibility(i, array);
            }

        });
    }

    private void visibility(int i, String[] array) {
        if (i == 0)
            prev.setVisibility(View.GONE);
        if (i == array.length - 1)
            next.setVisibility(View.GONE);
    }

And the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gastronomaapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.example.gastronomaapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <uses-permission android:name="com.example.gastronomaapp.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!--
         <application>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>








    -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ResterauntList"
            android:label="@string/title_activity_resteraunt_list" >
        </activity>
        <activity
            android:name=".Map"
            android:label="@string/title_activity_map" >
        </activity>
        <activity
            android:name=".Button1List"
            android:label="@string/title_activity_button1_list" >
        </activity>
        <activity
            android:name=".SingleRestraunt"
            android:label="@string/title_activity_single_restraunt" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAaC-32WynuOO6stpRaK6LSxRMwCuvgdE4" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".ReviewPage"
            android:label="@string/title_activity_review_page" >
        </activity>
        <activity
            android:name=".OfferPage"
            android:label="@string/title_activity_offer_page" >
        </activity>
        <activity
            android:name=".ImageMenu"
            android:label="@string/title_activity_image_menu" >
        </activity>
        <activity
            android:name=".MenuActivity"
            android:label="@string/title_activity_menu" >
        </activity>
    </application>

</manifest>
3
  • Please post your manifest and the MenuActivity code. Commented Sep 2, 2014 at 14:01
  • What is if (menu1 == null) menu[i] = menu1; in your done() method? Are you sure you want to pass null value to your array? Commented Sep 2, 2014 at 14:04
  • Actually i wanted to add a check because sometimes i get null arrays from parse itself. changed it to if(menu1!=null) and app even more broken. Crashes with null pointer exception at the line with menu[i] = menu1; Commented Sep 2, 2014 at 14:16

2 Answers 2

3

You have to do the following:

Intent i = new Intent("Activity")
i.putExtra(key, ArrayName)

Intent i = getIntent();
String[] array = i.getExtras().getString(keyname);

and that's it.

Sign up to request clarification or add additional context in comments.

1 Comment

The second line(which i assume goes in the receiving activity) returns type errors
0

NPE could probably be caused because of following:

  1. menu array is declared but not initialized. It should be:

      String menu [] = new String [SIZE_OF_MY_MENU_ARRAY];
    
  2. Ensure that the menu Array is NOT NULL.
  3. In your code,

    if (menu1 == null)
         menu[i] = menu1; // <--- Null value is assigned to your array. Avoid that
    

ORIGINAL ANSWER

If you are transferring the string array from say, ActivityA.java to ActivityB.java

In ActivityA.java,

String [] myStringArray = // Declare Here...

Bundle b=new Bundle();
b.putStringArray("MY_KEY", myStringArray);
Intent i=new Intent(context, ActivityA);
i.putExtras(b);

To Read the String Array in ActivityB.java

Bundle b=this.getIntent().getExtras();
String[] myStringArray =b.getStringArray("MY_KEY");

You have also used similar syntax in your question.

1 Comment

@Androider: Thanks for accepting as answer. If you have enough reputation, please also upvote since it will improve our tag score.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.