但是当我想要这个时:
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);
}
}
顺便说一句,正如我所说,它与提供链接中的静态名称一样工作得很好。但我想要动态方法。提前谢谢。