1


I got a problem with my android application..
Im using string-array to show items from there in TextView, but everytime i get NullpointerException, can you help me and show where im wrong?
Here is code:

public class test extends Activity {

        String [] myString ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);


        myString = getResources().getStringArray(R.array.myArray);


        String q = myString[1];

        TextView tv = (TextView) findViewById(R.id.text2);
        tv.setText(q);
    }
}

and my xml code:

 <string-array name="myArray">
    <item > string 1</item>

    <item > string 2</item>

    <item> string 3</item>

    <item > string 4</item>
 </string-array>

LogCat:

04-15 11:38:27.647: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.test}: java.lang.NullPointerException
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.os.Looper.loop(Looper.java:123)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-15 11:38:27.647: E/AndroidRuntime(285):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:38:27.647: E/AndroidRuntime(285):  at java.lang.reflect.Method.invoke(Method.java:521)
04-15 11:38:27.647: E/AndroidRuntime(285):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 11:38:27.647: E/AndroidRuntime(285):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 11:38:27.647: E/AndroidRuntime(285):  at dalvik.system.NativeStart.main(Native Method)
04-15 11:38:27.647: E/AndroidRuntime(285): Caused by: java.lang.NullPointerException
04-15 11:38:27.647: E/AndroidRuntime(285):  at com.example.app.test.onCreate(vovochka.java:29)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-15 11:38:27.647: E/AndroidRuntime(285):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
9
  • Did you do the clean and build? Commented Apr 15, 2013 at 11:50
  • 2
    please show your logcat as well Commented Apr 15, 2013 at 11:51
  • You have not closed the string tag </string> Commented Apr 15, 2013 at 11:51
  • show line number where exception is thrown. probably your view id is wrong or something like that, I'm sure that mistake is very simple. Commented Apr 15, 2013 at 11:53
  • 1
    please make sure you have TextView R.id.text2 in R.layout.text Commented Apr 15, 2013 at 12:05

5 Answers 5

2

Set the text in textview on button click.

In arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myArray">
<item > string 1</item>
<item > string 2</item>
<item> string 3</item>
<item > string 4</item>
</string-array>
</resources>

In your activity

String[] myString;
TextView _tv;
Button b;
String q;

In onCreate()

setContentView(R.layout.activity_main);
myString = getResources().getStringArray(R.array.myArray);
q = myString[1];
_tv = (TextView) findViewById( R.id.tv ); 
b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        _tv.setText(q);

    }

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

Comments

1

Try this:

mainActivity.java

public class MainActivity extends Activity {
    String [] myString ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myString = getResources().getStringArray(R.array.myArray);
        String q = myString[1];
        Log.e(q,"string");
        TextView tv = (TextView) findViewById(R.id.text1);
        tv.setText(q);
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:text="TextView" />

</RelativeLayout>

String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">sample</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="myArray">
        <item> string 1</item>
        <item> string 2</item>
        <item> string 3</item>
        <item> string 4</item>
    </string-array>

</resources>

Comments

0

Try something like this as your xml in values/array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="myArray">
    <item>string 2</item>
    <item>string 3</item>
    <item>string 4</item>
</string-array>

</resources>

Comments

0

First you check that text view is exists on that view, otherwise that has no error on string[].

myString = getResources().getStringArray(R.array.myArray);

String q = myString[1];

TextView tv = (TextView) findViewById(R.id.text2);
if(tv==null)
{
   System.out.println("text view is null");
}
else
{
    tv.setText(q);
}

You may check with above code..

Comments

0

this may Helps , in your Array

String q = myString[1];

change like

int pos = 1;

String q = myString[pos];

Comments

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.