0

我正在尝试将图像添加到每张图片下带有文本标签的网格视图。当我运行代码时,它总是强制关闭。我无法弄清楚代码有什么问题。

package com.petedroid.euro2012;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class psquads extends Activity 
{


@Override
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new ImageAdapter(this));
}


public class ImageAdapter extends BaseAdapter
{
   private Context mContext;

   public ImageAdapter(Context c) {
      mContext = c;
  }

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

  public Object getItem(int position) {
      return null;
  }

  public long getItemId(int position) {
      return 0;
  }

  public View getView(int position, View convertView, ViewGroup parent){
      View MyView = convertView;
      if (convertView == null) {

          //Inflate Layout
          LayoutInflater li = getLayoutInflater();
          MyView = li.inflate(R.layout.griditem, null);

          //Get Image
          ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
          iv.setImageResource(mThumbIds[position]);

          //Get Text
          TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
          tv.setText(names[position] );
      }  

     return MyView;
  }


 }

 //Images
 private Integer[] mThumbIds = {
        R.drawable.croatia, R.drawable.republic,
        R.drawable.denmark, R.drawable.england,
        R.drawable.france, R.drawable.germany,
        R.drawable.greece, R.drawable.ireland,
        R.drawable.italy, R.drawable.netherlands,
        R.drawable.poland, R.drawable.portugal,
        R.drawable.russia, R.drawable.spain,
        R.drawable.sweden, R.drawable.ukraine,};

 //Text
 private String[] names={
       "Croatia","Czech Republic","Denmark", "England",
       "France","Germany","Greece", "Ireland", 
       "Italy", "Netherlands", "Poland", "Portugal", 
       "Russia", "Spain", "Sweden", "Ukraine", };
}

这是日志

04-21 00:07:33.384: E/global(27521): Deprecated Thread methods are not supported.
04-21 00:07:33.384: E/global(27521): java.lang.UnsupportedOperationException
04-21 00:07:33.384: E/global(27521):    at java.lang.VMThread.stop(VMThread.java:85)
04-21 00:07:33.384: E/global(27521):    at java.lang.Thread.stop(Thread.java:1379)
04-21 00:07:33.384: E/global(27521):    at java.lang.Thread.stop(Thread.java:1344)
04-21 00:07:33.384: E/global(27521):    at com.petedroid.euro2012.SplashScreenActivity$1.run(SplashScreenActivity.java:35)
04-21 00:07:35.368: D/AndroidRuntime(27521): Shutting down VM
04-21 00:07:35.368: W/dalvikvm(27521): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
04-21 00:07:35.380: E/AndroidRuntime(27521): FATAL EXCEPTION: main
04-21 00:07:35.380: E/AndroidRuntime(27521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.petedroid.euro2012/com.petedroid.euro2012.psquads}: java.lang.NullPointerException
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.os.Looper.loop(Looper.java:123)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at java.lang.reflect.Method.invokeNative(Native Method)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at java.lang.reflect.Method.invoke(Method.java:521)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at dalvik.system.NativeStart.main(Native Method)
04-21 00:07:35.380: E/AndroidRuntime(27521): Caused by: java.lang.NullPointerException
04-21 00:07:35.380: E/AndroidRuntime(27521):    at com.petedroid.euro2012.psquads.onCreate(psquads.java:24)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-21 00:07:35.380: E/AndroidRuntime(27521):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-21 00:07:35.380: E/AndroidRuntime(27521):    ... 11 more
04-21 00:08:14.755: I/dalvikvm(27521): threadid=3: reacting to signal 3
04-21 00:08:17.415: E/dalvikvm(27521): Failed to write stack traces to /data/anr/traces.txt (-1 of 2650): Math result not representable
04-21 00:08:26.317: I/Process(27521): Sending signal. PID: 27521 SIG: 9 
4

1 回答 1

1

我只能看到两件事...如果您的代码行与我的计数一致,那么findViewById()您的 gridView 似乎返回 null,请确保它在您的布局中,如所述。其次看起来你在你的 splashScreen 中调用 Thread.stop() ,并且 Thread.stop() 没有在 Dalvik VM 中实现。

于 2012-04-20T23:26:20.067 回答