gpt4 book ai didi

java - Android:如何使用图像按钮?

转载 作者:行者123 更新时间:2023-12-02 05:48:16 25 4
gpt4 key购买 nike

我尝试在 Android 应用程序上使用图像按钮,但每次在手机上运行它时都会崩溃。这是我正在使用的代码。有人能帮我吗?我想知道我哪里搞砸了。如果我的代码中遗漏了某些内容或其他内容。任何建议将不胜感激。谢谢。

编辑:添加了 XML 代码

编辑*:添加了 Logcat

代码:

ImageButton emergencia_nueve = (ImageButton) findViewById(R.id.telefono_nine);
emergencia_nueve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
String number="911";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
});

XML:

 <ImageButton
android:id="@+id/telefono_nine"
android:layout_width="@android:dimen/app_icon_size"
android:layout_height="@android:dimen/app_icon_size"
android:layout_alignStart="@+id/alarmaizq"
android:layout_below="@+id/alarmaizq"
android:layout_marginStart="12dp"
android:layout_marginTop="26dp"
android:adjustViewBounds="true"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:src="@drawable/phone"
android:onClick="telefono_nine"
app:srcCompat="@drawable/phone" />

日志猫:

04-28 08:44:24.199 1237-1237/? I/TrafficMonitor: gettimer:interval=2000
04-28 08:44:24.660 1237-1353/? E/libfingersense_wrapper: libfingersense.so was not loaded
04-28 08:44:24.768 1237-1353/? E/libfingersense_wrapper: libfingersense.so was not loaded
04-28 08:44:24.802 15853-15853/? E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
04-28 08:44:24.805 15853-15853/? E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
04-28 08:44:24.820 15853-15853/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: e.danma.drawer, PID: 15853
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at e.danma.drawer.MainActivity.onNavigationItemSelected(MainActivity.java:107)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:154)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22459)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
04-28 08:44:24.830 1237-1237/? I/SendBroadcastPermission: action:android.intent.action.DROPBOX_ENTRY_ADDED, mPermissionType:0
04-28 08:44:24.832 1237-2988/? E/ReportTools: This is not beta user build
04-28 08:44:24.838 1237-1250/? W/RMS.SmartShrinker: SmartShrinker is runing in pid =24275 reclaim mode = 4
04-28 08:44:24.839 1237-1250/? W/RMS.SmartShrinker: SmartShrinker is runing in pid =12011 reclaim mode = 4
04-28 08:44:24.839 1237-1250/? W/RMS.SmartShrinker: SmartShrinker is runing in pid =3851 reclaim mode = 4
04-28 08:44:24.839 1237-1250/? W/RMS.SmartShrinker: SmartShrinker is runing in pid =14034 reclaim mode = 4
04-28 08:44:24.840 15853-15853/? I/Process: Sending signal. PID: 15853 SIG: 9
04-28 08:44:25.024 1237-9699/? I/SendBroadcastPermission: action:android.intent.action.SIG_STR, mPermissionType:0
04-28 08:44:25.024 1237-1237/? I/MQoS: onSignal: mSubId=0,currDataSubID=0
04-28 08:44:25.024 1237-1237/? I/MQoS: received cell-signal:5
04-28 08:44:25.026 407-418/? I/gralloc: alloc_device_free:586: Free handle(0x783f0a2a00)
04-28 08:44:25.026 407-418/? I/gralloc: alloc_device_free:586: Free handle(0x783f105100)
04-28 08:44:25.028 1237-20357/? E/HsmCoreServiceImpl: onTransact in code is: 102
04-28 08:44:25.028 1237-20357/? I/MediaProcessHandler: processOp opType: 1, uid: 10179, pid: 15853
04-28 08:44:25.028 1237-20357/? W/MediaProcessHandler: remove target not exist, maybe the UI process: uid: 10179, pid: 15853
04-28 08:44:25.031 1547-1632/? I/HwMobileSignalController: subId:0 phoneType:1 networktype:3 targetClass:2 masterLevel:5 slaveLevel:-1
04-28 08:44:25.033 1237-1352/? W/InputDispatcher: channel 'c92dc14 e.danma.drawer/e.danma.drawer.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
04-28 08:44:25.033 1237-1352/? E/InputDispatcher: channel 'c92dc14 e.danma.drawer/e.danma.drawer.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-28 08:44:25.085 1237-1367/? E/WifiConfigManager: updateConfiguration freq=2437 BSSID=58:23:8c:6a:96:5a RSSI=-45 "HOME-965A"WPA_PSK
04-28 08:44:25.090 1237-4023/? W/InputDispatcher: Attempted to unregister already unregistered input channel 'c92dc14 e.danma.drawer/e.danma.drawer.MainActivity (server)'
04-28 08:44:25.106 407-407/? I/gralloc: alloc_device_free:586: Free handle(0x783f0a2000)
04-28 08:44:25.129 2507-2507/? I/HwLauncher: Launcher onStart()
04-28 08:44:25.129 2507-2507/? I/HwLauncher: Launcher Launcher.onStart() cost 0 ms
04-28 08:44:25.129 2507-2507/? I/HwLauncher: Launcher onResume()
04-28 08:44:25.134 2507-2507/? I/HwLauncher: Launcher doResumeWork()
04-28 08:44:25.148 2507-2507/? I/HwLauncher: Launcher onResume mIsToUninstallApp = false
04-28 08:44:25.148 2507-2507/? I/HwLauncher: Launcher Launcher hot start cost 19 ms
04-28 08:44:25.159 2507-2752/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-28 08:44:25.159 2507-2644/? E/HwLauncher: SettingsEx , no such field.
04-28 08:44:25.162 2507-2644/? W/HwLauncher: Clock getDataFormat the getSystemString failed.
04-28 08:44:25.171 407-1449/? I/gralloc: alloc_device_alloc:564: Alloc handle(0x783f0a2f00): interfmt=200000001, stride=1088, size=8486912, usage=0xb00
04-28 08:44:25.178 407-1449/? I/gralloc: alloc_device_alloc:564: Alloc handle(0x783f037e00): interfmt=200000001, stride=1088, size=8486912, usage=0xb00
04-28 08:44:25.181 1547-1547/? I/PhoneStatusBarPolicy: updateBluetooth:::setBluetoothBatteryEnable false.
04-28 08:44:25.181 1547-1547/? I/PhoneStatusBarPolicy: updateBluetooth::bluetoothEnabled:false, bluetoothConnected=false, bluetoothTransfering=false
04-28 08:44:25.184 407-1449/? I/gralloc: alloc_device_alloc:564: Alloc handle(0x783f037200): interfmt=200000001, stride=1088, size=8486912, usage=0xb00
04-28 08:44:25.190 2507-2752/? E/OpenGLRenderer: allen debug liyu Key: 1
04-28 08:44:25.199 2507-2752/? E/OpenGLRenderer: allen debug liyu Key: 34359738371
04-28 08:44:25.201 2507-2752/? E/OpenGLRenderer: allen debug liyu Key: 68719476744
04-28 08:44:25.203 2507-2752/? E/OpenGLRenderer: allen debug liyu Key: 68719476736
04-28 08:44:25.230 2507-2507/? I/HwLauncher: Launcher.Utilities unread scaledHeight = 54;sBigIconSize = 172
04-28 08:44:25.269 1237-1353/? E/libfingersense_wrapper: libfingersense.so was not loaded
04-28 08:44:25.282 2507-2507/? I/HwLauncher: FPSMonitor [Launcher , dispatchTouchEvent begin, false, true] down event x = 355 y = 661
04-28 08:44:25.282 2507-2507/? I/HwLauncher: Launcher workspaceTranslationX = 0.0
04-28 08:44:25.283 2507-2507/? I/HwLauncher: FPSMonitor [PagedView , onIntercept] down event x = 355 y = 661
04-28 08:44:25.284 2507-2507/? I/HwLauncher: FPSMonitor [Launcher , dispatchTouchEvent end, false, true] down event x = 355 y = 661
04-28 08:44:25.327 1237-1353/? E/libfingersense_wrapper: libfingersense.so was not loaded
04-28 08:44:25.328 2507-2507/? I/HwLauncher: FPSMonitor [Launcher , dispatchTouchEvent begin, false, true] up event x = 355 y = 661
04-28 08:44:25.329 2507-2507/? I/HwLauncher: FPSMonitor [PagedView , onIntercept] up event x = 355 y = 661
04-28 08:44:25.329 2507-2507/? I/HwLauncher: FPSMonitor [Launcher , dispatchTouchEvent end, false, true] up event x = 355 y = 661
04-28 08:44:25.699 2507-2752/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
04-28 08:44:25.699 2507-2752/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
04-28 08:44:26.201 1237-1237/? I/TrafficMonitor: update:rxPkts:0,txPkts:0,rxBytes:0,txBytes:0
04-28 08:44:26.201 1237-1237/? I/TrafficMonitor: start expired. level:-1

最佳答案

首先膨胀放置图像按钮的 XML。您可以在主 Activity 中通过调用该 xml 的设置内容 View 来完成此操作。

setContentView(R.layout.your_layout_name);

然后执行其余代码实现。

也在 Activity 的 onCreate 中执行此操作。

关于java - Android:如何使用图像按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50076772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com