ボタンが押されるたびにプロンプトを表示しようとしていますが、(チュートリアルを使用して) 3 つのボタンのうち最初のボタンでAlertDialog
のみ正常に実装できました。AlertDialog
追加したら:
button = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button3);
どのボタンも機能せず、アプリケーション フォースが終了します。簡単なことを見落としているか、単にこれを正しくコーディングしていないことはわかっています。
(どんな助けでも大歓迎です!)
ジャワ:
package com.example.linkingmanager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AppActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newdevicebtn:
startActivity(new Intent(this, App2Activity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
final Context context = this;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button3);
// add button listener
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Settings Menu");
// set dialog message
alertDialogBuilder
.setMessage("Delete Edit or Link?")
.setCancelable(false)
.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//start new activity
Intent intentApp2Activity = new Intent(AppActivity.this, User1Settings.class);
startActivity(intentApp2Activity);
}
})
.setPositiveButton("Link",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//start new activity
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
// if this button is clicked, close
// current activity
AppActivity.this.finish();
}
})
.setNegativeButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/link_devices"
android:orientation="vertical" >
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 1" android:onClick="onPopupBtClick" />
<Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 2" android:onClick="onPopupBtClick" />
<Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 3" android:onClick="onPopupBtClick" />
</LinearLayout>
ログキャット:
03-12 21:22:28.194: E/Trace(1648): error opening trace file: No such file or directory (2)
03-12 21:22:28.714: D/dalvikvm(1648): GC_FOR_ALLOC freed 64K, 8% free 2467K/2676K, paused 83ms, total 86ms
03-12 21:22:28.794: I/dalvikvm-heap(1648): Grow heap (frag case) to 6.062MB for 3686416-byte allocation
03-12 21:22:28.854: D/dalvikvm(1648): GC_FOR_ALLOC freed 2K, 4% free 6065K/6280K, paused 53ms, total 53ms
03-12 21:22:28.971: D/dalvikvm(1648): GC_CONCURRENT freed <1K, 4% free 6065K/6280K, paused 8ms+7ms, total 118ms
03-12 21:22:30.784: D/libEGL(1648): loaded /system/lib/egl/libEGL_emulation.so
03-12 21:22:30.793: D/(1648): HostConnection::get() New Host Connection established 0x2a1a5180, tid 1648
03-12 21:22:30.885: D/libEGL(1648): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-12 21:22:30.904: D/libEGL(1648): loaded /system/lib/egl/libGLESv2_emulation.so
03-12 21:22:31.024: W/EGL_emulation(1648): eglSurfaceAttrib not implemented
03-12 21:22:31.055: D/OpenGLRenderer(1648): Enabling debug mode 0
03-12 21:22:32.973: D/dalvikvm(1648): GC_FOR_ALLOC freed 12K, 3% free 6125K/6280K, paused 84ms, total 99ms
03-12 21:22:33.063: I/dalvikvm-heap(1648): Grow heap (frag case) to 9.634MB for 3686416-byte allocation
03-12 21:22:33.333: D/dalvikvm(1648): GC_CONCURRENT freed 2K, 2% free 9722K/9884K, paused 110ms+8ms, total 261ms
03-12 21:22:35.783: W/EGL_emulation(1648): eglSurfaceAttrib not implemented
03-12 21:25:49.243: D/AndroidRuntime(1648): Shutting down VM
03-12 21:25:49.243: W/dalvikvm(1648): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-12 21:25:49.393: E/AndroidRuntime(1648): FATAL EXCEPTION: main
03-12 21:25:49.393: E/AndroidRuntime(1648): java.lang.IllegalStateException: Could not find a method onPopupBtClick(View) in the activity class com.example.linkingmanager.AppActivity for onClick handler on view class android.widget.Button with id 'button1'
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$1.onClick(View.java:3586)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View.performClick(View.java:4204)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$PerformClick.run(View.java:17355)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Handler.handleCallback(Handler.java:725)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Looper.loop(Looper.java:137)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 21:25:49.393: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 21:25:49.393: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 21:25:49.393: E/AndroidRuntime(1648): at dalvik.system.NativeStart.main(Native Method)
03-12 21:25:49.393: E/AndroidRuntime(1648): Caused by: java.lang.NoSuchMethodException: onPopupBtClick [class android.view.View]
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.Class.getMethod(Class.java:915)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$1.onClick(View.java:3579)
03-12 21:25:49.393: E/AndroidRuntime(1648): ... 11 more