android - 由于将静态数组列表更改为动态而崩溃

时间:2013-04-22 15:04:27

标签: android android-viewpager

我已经用过了 Different text for each image in image viewpager

但是当我想要这个时:

public String[] stringArray = getResources().getStringArray(R.array.myarry);

而不是使用静态

stringArray = new String[] { "Image a", "Image b","Image c","Image d","Image e"};

应用程序崩溃,我真的不知道为什么。

这是我的LogCat:

04-22 14:56:37.002: D/dalvikvm(1066): GC freed 954 objects / 73824 bytes in 121ms
04-22 14:56:37.142: D/AndroidRuntime(1066): Shutting down VM
04-22 14:56:37.142: W/dalvikvm(1066): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-22 14:56:37.152: E/AndroidRuntime(1066): Uncaught handler: thread main exiting due to uncaught exception
04-22 14:56:37.182: E/AndroidRuntime(1066): java.lang.NullPointerException
04-22 14:56:37.182: E/AndroidRuntime(1066):     at com.mypackage.pager.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:38)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.support.v4.view.ViewPager.populate(ViewPager.java:929)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.View.measure(View.java:7964)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.View.measure(View.java:7964)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.View.measure(View.java:7964)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.View.measure(View.java:7964)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.View.measure(View.java:7964)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.os.Looper.loop(Looper.java:123)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at java.lang.reflect.Method.invoke(Method.java:521)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-22 14:56:37.182: E/AndroidRuntime(1066):     at dalvik.system.NativeStart.main(Native Method)
04-22 14:56:37.232: I/dalvikvm(1066): threadid=7: reacting to signal 3
04-22 14:56:37.232: E/dalvikvm(1066): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

这也是我的ImagePagerAdapter

package com.mypackage.pager;

import android.app.Activity;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ImagePagerAdapter extends PagerAdapter {

    Activity activity;
    int imageArray[];
    String[] stringArray;

    public ImagePagerAdapter(Activity act, int[] imgArra, String[] StringArra) {
        imageArray = imgArra;
        activity = act;
        stringArray = StringArra;
    }

    public int getCount() {
        return imageArray.length;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.custom_pager, null);

        ImageView im = (ImageView) layout.findViewById(R.id.myimage);
        im.setImageResource(imageArray[position]);

        TextView txt = (TextView) layout.findViewById(R.id.image_text);
        txt.setText(stringArray[position]);  **//Line 38**
        ((ViewPager) collection).addView(layout, 0);
        return layout;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

和ImagePager

package com.mypackage.pager;

import com.mypackage.pager.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class ImagePager extends Activity {


    public int imageArra[] = { R.drawable.p4a_001, R.drawable.p4a_002, R.drawable.p4a_003,
            R.drawable.p4a_004, R.drawable.p4a_005,R.drawable.p4a_006,R.drawable.p4a_007 };
    public String[] stringArray ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra,
                stringArray);
        ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

        stringArray = getResources().getStringArray(R.array.myarray);


    }


}

BTW,因为我说它可以正常使用静态名称,如提供链接中的名称。 但我想提前动态方法。谢谢。

2 个答案:

答案 0 :(得分:1)

在使用stringArray之前添加语句stringArray = getResources().getStringArray(R.array.myarray);,即ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra,stringArray);。如果没有这个,您将null值传递给ImagePagerAdapter。

即,onCreate()代码必须如下所示

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stringArray = getResources().getStringArray(R.array.myarray); 
        ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra,
                stringArray);
        ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

答案 1 :(得分:0)

尝试在使用之前初始化String数组。