debug测试的时候categoryArray[i]明明有值啊,可是为什么就是传不到list里面呢
06-14 09:33:00.830: E/AndroidRuntime(736): FATAL EXCEPTION: main 06-14 09:33:00.830: E/AndroidRuntime(736): java.lang.RuntimeException: Unable to start activity ComponentInfo{napo.news.activity/napo.news.activity.WenpingNewsActivity}: java.lang.NullPointerException 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.os.Handler.dispatchMessage(Handler.java:99) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.os.Looper.loop(Looper.java:123) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread.main(ActivityThread.java:4627) 06-14 09:33:00.830: E/AndroidRuntime(736): at java.lang.reflect.Method.invokeNative(Native Method) 06-14 09:33:00.830: E/AndroidRuntime(736): at java.lang.reflect.Method.invoke(Method.java:521) 06-14 09:33:00.830: E/AndroidRuntime(736): at 毕业论文 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 06-14 09:33:00.830: E/AndroidRuntime(736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 06-14 09:33:00.830: E/AndroidRuntime(736): at dalvik.system.NativeStart.main(Native Method) 06-14 09:33:00.830: E/AndroidRuntime(736): Caused by: java.lang.NullPointerException 06-14 09:33:00.830: E/AndroidRuntime(736): at napo.news.activity.WenpingNewsActivity.onCreate(WenpingNewsActivity.java:55) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 06-14 09:33:00.830: E/AndroidRuntime(736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 06-14 09:33:00.830: E/AndroidRuntime(736): ... 11 more
package napo.news.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import napo.news.util.DensityUtil; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.ViewGroup.LayoutParams; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.SimpleAdapter; public class WenpingNewsActivity extends Activity { private final int COLUMNWIDTHPX = 55; private int mColumnWidthDip; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mColumnWidthDip = DensityUtil.px2dip(this, COLUMNWIDTHPX); String[] categoryArray = getResources().getStringArray( R.array.categories); List<HashMap<String, String>> categories = new ArrayList<HashMap<String, String>>(); for (int i=0; i < categoryArray.length; i++) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("category_title", categoryArray[i]); categories.add(hashMap); } SimpleAdapter categoryAdapter = new SimpleAdapter(this, categories, R.layout.category_title, new String[]{"category_title"}, new int[] {R.id.category_title}); GridView category = new GridView(this); category.setColumnWidth(mColumnWidthDip); category.setNumColumns(GridView.AUTO_FIT); category.setGravity(Gravity.CENTER); int width = mColumnWidthDip * categories.size(); LayoutParams params = new LayoutParams(width, LayoutParams.WRAP_CONTENT); category.setLayoutParams(params); category.setAdapter(categoryAdapter); LinearLayout categoryList = (LinearLayout)findViewById(R.id.catgory_layout); categoryList.addView(category); } }
定义 private int mColumnWidthDip;直接定义成 private int mColumnWidthDip=0;不就行了吗