0

ダイヤラーのようなアクティビティを開発しようとしています。これは私がこれまでに持っているコードです...

public class Dial extends Activity implements OnClickListener {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dial);


    Button pb1 = (Button) findViewById(R.id.pb1);
    Button pb2 = (Button) findViewById(R.id.pb2);
    Button pb3 = (Button) findViewById(R.id.pb3);
    Button pb4 = (Button) findViewById(R.id.pb4);
    Button pb5 = (Button) findViewById(R.id.pb5);
    Button pb6 = (Button) findViewById(R.id.pb6);
    Button pb7 = (Button) findViewById(R.id.pb7);
    Button pb8 = (Button) findViewById(R.id.pb8);
    Button pb9 = (Button) findViewById(R.id.pb9);
    Button pb0 = (Button) findViewById(R.id.pb0);
    Button pbstar = (Button) findViewById(R.id.pbstar);
    Button pbhash = (Button) findViewById(R.id.pbhash);
    Button pbcall = (Button) findViewById(R.id.pbcall);


    pb1.setOnClickListener(this);
    pb2.setOnClickListener(this);
    pb3.setOnClickListener(this);
    pb4.setOnClickListener(this);
    pb5.setOnClickListener(this);
    pb6.setOnClickListener(this);
    pb7.setOnClickListener(this);
    pb8.setOnClickListener(this);
    pb9.setOnClickListener(this);
    pb0.setOnClickListener(this);
    pbstar.setOnClickListener(this);
    pbhash.setOnClickListener(this);



}


@Override
public void onClick(View v)
{

    TextView text=(TextView)findViewById(R.id.text);

      int id =  (v.getId());

      if (id == R.id.pb1){        
            text.append("1");
      }

      if (id == R.id.pb2){        
          text.append("2");
  }
      if (id == R.id.pb3){        
          text.append("3");
  }
      if (id == R.id.pb4){        
          text.append("4");
  }
      if (id == R.id.pb5){        
          text.append("5");
  }
      if (id == R.id.pb6){        
          text.append("6");
  }
      if (id == R.id.pb7){        
          text.append("7");
  }
      if (id == R.id.pb8){        
          text.append("8");
  }
      if (id == R.id.pb9){        
          text.append("9");
  }
      if (id == R.id.pb0){        
          text.append("0");
  }


}

}

このコードで、アプリを強制終了します。ただし、この部分を削除すると:

Button pb1 = (Button) findViewById(R.id.pb1);
Button pb2 = (Button) findViewById(R.id.pb2);
Button pb3 = (Button) findViewById(R.id.pb3);
Button pb4 = (Button) findViewById(R.id.pb4);
Button pb5 = (Button) findViewById(R.id.pb5);
Button pb6 = (Button) findViewById(R.id.pb6);
Button pb7 = (Button) findViewById(R.id.pb7);
Button pb8 = (Button) findViewById(R.id.pb8);
Button pb9 = (Button) findViewById(R.id.pb9);
Button pb0 = (Button) findViewById(R.id.pb0);
Button pbstar = (Button) findViewById(R.id.pbstar);
Button pbhash = (Button) findViewById(R.id.pbhash);
Button pbcall = (Button) findViewById(R.id.pbcall);


pb1.setOnClickListener(this);
pb2.setOnClickListener(this);
pb3.setOnClickListener(this);
pb4.setOnClickListener(this);
pb5.setOnClickListener(this);
pb6.setOnClickListener(this);
pb7.setOnClickListener(this);
pb8.setOnClickListener(this);
pb9.setOnClickListener(this);
pb0.setOnClickListener(this);
pbstar.setOnClickListener(this);
pbhash.setOnClickListener(this);

アプリは正常に起動しますが、何もしません (onClickListener がないため)。これらの宣言行にエラーがありますか? それらを変更しようとしましたが、まだ何もありません...基本的に欲しいのは、ボタンを押すと、それに割り当てられた番号が(テキストビューで)表示されることです...この問題を解決するにはどうすればよいですか? 初心者の質問で申し訳ありませんが、私は今始めているので、この問題を解決したいと思っています。

ログキャット:

11-06 14:42:04.358: I/dalvikvm-heap(1658): Grow heap (frag case) to 52.477MB for 10074256-byte         allocation
11-06 14:42:04.518: I/dalvikvm-heap(1658): Grow heap (frag case) to 62.144MB for 10126096-byte allocation
11-06 14:42:04.668: I/dalvikvm-heap(1658): Grow heap (frag case) to 71.617MB for 9970576-byte allocation
11-06 14:42:10.144: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception  (group=0x417e5898)
11-06 14:42:10.154: E/AndroidRuntime(1658): FATAL EXCEPTION: main
11-06 14:42:10.154: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.devon.carmate/com.devon.carmate.Dial}: java.lang.ClassCastException:  android.widget.ImageButton cannot be cast to android.widget.Button
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.ActivityThread.access$600(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.os.Handler.dispatchMessage(Handler.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.os.Looper.loop(Looper.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.ActivityThread.main(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at java.lang.reflect.Method.invoke(Method.java:525)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at dalvik.system.NativeStart.main(Native Method)
11-06 14:42:10.154: E/AndroidRuntime(1658): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
11-06 14:42:10.154: E/AndroidRuntime(1658):     at com.devon.carmate.Dial.onCreate(Dial.java:23)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.Activity.performCreate(Activity.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
11-06 14:42:10.154: E/AndroidRuntime(1658):     ... 13 more
4

1 に答える 1

0

ここに問題があります

11-06 14:42:10.154: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.devon.carmate/com.devon.carmate.Dial}: java.lang.ClassCastException:  android.widget.ImageButton cannot be cast to android.widget.Button

Buttonxml で s をs として宣言していますが、Java でImageButtonキャストしようとしています。ButtonそれらをButtonxmlでに変更するかImageButton、Java Fでに変更します

たとえば、代わりに

 Button pb1 = (Button) findViewById(R.id.pb1);

に変更します

 ImageButton pb1 = (ImageButton) findViewById(R.id.pb1);

リスナーの異なる実装

Java ですべての を宣言してButtonを割り当てる代わりにlistener、xml で行うことができます。Buttonこれは、すべてのが同じonClick()イベントを使用するこのような状況でうまく機能します。これにより、Java のコードがクリーンアップされ、読みやすくなり、エラーのリスクが軽減されます。あなたがする必要があるのは、それぞれまたはxmlのonClickプロパティをこのような関数に割り当てることですButtonImageButton

<Button 
    ...
    android:onClick="someFunction"/>

上記の「...」は、他のプロパティがある場所です。次に、Java では、追加する必要implements OnClickListenerはなく、すべての宣言やlistener. 適切なルールに従って関数を定義するだけです

  1. 公開する必要があります
  2. ビューである 1 つのパラメーターを取る必要があります

のようなもの

public void someFunction(View v) //just use the same name as in your xml
{
    int id = v.getId();
    if (id == R.id.pb1){        
        text.append("1");
    }

    if (id == R.id.pb2){        
      text.append("2");
     // etc
}

より読みやすくするswitchステートメントを使用することもできます、私見。id

この方法の詳細については、Button ドキュメントを参照してください。

于 2013-11-06T18:48:47.947 に答える