Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java: Using string resource in external class

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java: Using string resource in external class

    Hi everyone, am new to Java. During development I needed to use string resources. I can fetch them from strings.xml for the layout xml file and for the MainActivity.java file (using getString(R.string.name)). But I can't get to use them in an external class file (which is being used to handle all the functions of the MainActivity file).

    I have gone through StackOverflow forum and tried many answers but in vain. I tried using:

    1. Resources.getSystem().getString(R.string.name)
    2. getResources.getString(R.string.name)

    I have a orderProcessing button in mainActivity.java which gets processed in orderSummary function in mathCalculator.java. Everything works out fine when I use hardcoded strings. I just can't get to use the string resources from strings.xml to reference them.

    Please help out in this regard. Many thanks

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java: Using string resource in external class

    Please post the code you are working with. Be sure to wrap the code in code tags.
    Also post a sample input file.

    Is this an Android app? If so, it will be moved to the Android section.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java: Using string resource in external class

    This is my "strings.xml" file:

    <code>
    <resources xmlns: xliff="urnasis:names:tcliff:document:1.2">
    <string name="app_name">Java Doughnut Corner</string>
    <string name="name">Name: <xliff:g id="name">%s</xliff:g></string>
    <string name="doughnuts"><xliff:g id="doughnuts">%i</xliff:g> Doughnuts ordered.</string>
    <string name="total">Total Price: £<xliff:g id="total">%i</xliff:g></string>
    <string name="thanks">Thank You...</string>
    </resources>
    </code>

    This is the onClickListener from "MainActivity.java" file which calls upon the function in external class:

    <code>
    order.setOnClickListener(view -> {
    MathCalculator calculator = new MathCalculator();
    calculator.setCustomer(String.valueOf(name.getText ()));
    result.setText(calculator.summary(calculator.addit ivePrice(additive)));
    }
    calculator.setSummary("");
    });
    </code>

    This is the function from "mathCalculator.java" file (external class):

    <code>
    public String summary(int additiveTotal){
    String message = additives();
    summary = getString(R.string.name, customer) + "\n";
    summary += message + "\n";
    summary += getString(R.string.doughnuts, quantity) + "\n";
    summary += getString(R.string.total, additiveTotal) + "\n";
    summary+= getString(R.string.thanks);
    return summary;
    }
    </code>

    Yes, this is an Android App.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java: Using string resource in external class

    What folder is the strings.xml file in?

    Does the posted code compile without errors?
    What is returned by the getString method? Have you printed the value returned with a print statement? The print out will show on the log cat.

    Did you look at the API doc to see what class the getString method was in? You need a reference to that class to be able to call its methods.


    Note: code tags use [] NOT <>
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java: Using string resource in external class

    I am using Android Studio for Java development. The strings.xml file is in res folder.

    The posted code compiles without errors but the app crashes when reaching the said function. The getString method doesn't print anything in the log when it gets executed because the app crashes on the statement.

    The getString method is a native method and i am not sure to which class it belongs.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java: Using string resource in external class

    the app crashes
    Please copy the full stack trace from the log cat that shows what the error was and where it happened.

    getString method is a native method
    I do not think there is anything like a native method. All methods belong to some class. Look in the API doc and find what class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java: Using string resource in external class

    Also posted here: https://coderanch.com/t/745172/java/...external-class

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Javadoc link to external resource
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: March 18th, 2014, 08:46 AM
  2. issue with scanner class-resource leak
    By davep1983 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2014, 03:00 PM
  3. Problem with external class
    By outime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2013, 12:15 PM
  4. use external class
    By swedishzeus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2013, 06:53 AM
  5. Can't call method from external class
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 24th, 2012, 08:30 PM

Tags for this Thread