1


我的 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>

日志猫:

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

5 回答 5

2

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

在数组.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);

    }

});
于 2013-04-15T11:59:26.013 回答
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);
    }
}

主要的.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>

字符串.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>
于 2013-04-15T12:04:20.707 回答
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>
于 2013-04-15T11:54:14.247 回答
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);
}

您可以检查上面的代码..

于 2013-04-15T12:04:06.297 回答
0

这可能对您有帮助Array

String q = myString[1];

改变喜欢

int pos = 1;

String q = myString[pos];
于 2013-04-15T12:08:22.163 回答