0

ハードコードの代わりに単純なループを使用してコードを最適化することにしました。これは、以前に各 PickView を個別に実装し、正常に機能したためです。

問題のある場所は次のとおりです。

pick = new PickView[9];
int id;
for(int i = 0; i < pick.length; i++) {
    id = getResources().getIdentifier("pick_" + i, "id", getPackageName());
    pick[i] = (PickView)findViewById(id);
    pick[i].setOnTouchListener(this); //this is MainActivity, line:54
}

...予期せず NullPointerException を受け取りました:

ERROR/AndroidRuntime(8450): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.***.onCreate(MainActivity.java:54)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

PickView クラスを囲みます。

public class PickView extends RelativeLayout {

    private TextView textView;
    private ImageView imageView;
    private String text;

    public PickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PickView);
        text = a.getString(R.styleable.PickView_text);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.pick_view, this);
        textView = (TextView)findViewById(R.id.pick_text);
        imageView = (ImageView)findViewById(R.id.pick_bg);
    }

    public void setText(String text) {
        textView.setText(text);
    }

    public void activate(boolean isActive) {
        if (isActive)
            imageView.setImageResource(R.drawable.pick_active);
        else
            imageView.setImageResource(R.drawable.pick);
    }
}

どんな助けにも感謝します!

4

3 に答える 3

1

多分あなたはそれをそのように見つけることができます:

for (int i = 0; i < pick.length; i++)
{
  resources = getResources();
  if(resources == null)
  {
    System.out.println("oops.. no resources");
    throw new RuntimeException("oops... no resources");
  }
  id = resources.getIdentifier("pick_" + i, "id", getPackageName());
  pickView = (PickView) findViewById(id);
  if(resources == null)
  {
    System.out.println("oops.. noo pickView");
    throw new RuntimeException("oops... no pickView");
  }
  pick[i] = pickView;
  pick[i].setOnTouchListener(this); //this is MainActivity, line:54
}
于 2012-09-06T13:32:57.060 に答える
1

ちょっとした質問ですが、pick_0 リソースはありますか?

とにかく、実際に ID を取得していることと、返された PickView が null でないことを確認する必要がありますか?

于 2012-09-06T13:25:38.127 に答える
1

Java doc ofは、問題の原因であることが見つからない場合getIdentifierに戻ることを示唆しています。0見つからない場合はfindViewById戻りますnull

public int getIdentifier (String name, String defType, String defPackage)

指定されたリソース名のリソース識別子を返します。完全修飾リソース名は、「package:type/entry」の形式です。ここで defType と defPackage がそれぞれ指定されている場合、最初の 2 つのコンポーネント (パッケージとタイプ) はオプションです。

注: この関数の使用は推奨されません。名前でリソースを取得するよりも、識別子でリソースを取得する方がはるかに効率的です。

int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

于 2012-09-06T13:25:07.003 に答える