1

こんにちは、私は AVD を使用していますが、取得時にスタックをエラーにする方法がわかりません -> アプリが強制的に終了しました...

シンプルなリスナーをコーディングしたかったのですが、エラーが発生しました -> setOnClick リスナーが原因でエラーが発生したことはわかっており、これは null ポインター例外による可能性がありますが、スタック エラーで確認したいと考えています。例外がある理由も教えてください。XMLの部分を添付します

package com.example.pierwsza;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;

class MainActivity extends Activity implements OnClickListener{
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button3);
   // b.setOnClickListener((android.view.View.OnClickListener) this); <--- ERROR
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(DialogInterface dialog, int which) {
    //b.setText("Ale to dziwne");

}


}

その BUTTON の XML

 <Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/linearLayout1"
    android:layout_marginBottom="29dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="Jakis przycisk" />
4

2 に答える 2

2

使用する

b = (Button) findViewById(R.id.button3);
b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    }); 

それ以外の

b = (Button) findViewById(R.id.button3);
b.setOnClickListener((android.view.View.OnClickListener) this); 

また

b = (Button) findViewById(R.id.button3);
b.setOnClickListener((this); 

メソッドを次のように変更します。

public void onClick(View view) {
    //b.setText("Ale to dziwne");
}

また、インポート

import android.view.View.OnClickListener;

それ以外の

import android.content.DialogInterface.OnClickListener;
于 2012-07-01T18:39:00.873 に答える
2

Eclipse を使用してコーディングしている場合は、[ウィンドウ] -> [ビューの表示] -> [LogCat]に移動して、アプリによってスローされたエラー スタックを確認します。

この特定のエラーを修正するには、これを変更します。

public void onClick(DialogInterface dialog, int which) {
    //b.setText("Ale to dziwne");
}

View.OnClickListener() を次のように使用するには:

public void onClick(View view) {
    //b.setText("Ale to dziwne");
}

Ctrl+ Shift+を押しOて、インポート ステートメントを DialogInterface.OnClickListener から View.OnClickListener に更新します。

于 2012-07-01T18:41:30.607 に答える