字符串数组的NullpointerException

时间:2013-04-15 11:48:23

标签: android nullpointerexception arrays


我的Android应用程序出了问题。
我使用字符串数组在TextView中显示项目,但每次我得到NullpointerException,你能帮助我并显示我错在哪里吗? 这是代码:

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

和我的xml代码:

 <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)

5 个答案:

答案 0 :(得分:2)

单击按钮,在textview中设置文本。

在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>

在您的活动中

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

在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);

    }

});

答案 1 :(得分:1)

试试这个:

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>

答案 2 :(得分:0)

在values / array.xml中尝试这样的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>

答案 3 :(得分:0)

首先检查该视图上是否存在文本视图,否则字符串[]上没有错误。

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

您可以查看以上代码..

答案 4 :(得分:0)

这可能有助于您的Array

String q = myString[1];

改变如

int pos = 1;

String q = myString[pos];