前提・実現したいこと
Androidstudioで次のものを作成したいです。
①「QRカメラ起動」ボタンを押してカメラを起動する。
②カメラ上でQRコードを読み込み結果を表示する。
発生している問題・エラーメッセージ
①アプリを起動時、「QRカメラ起動」ボタンが表示されずいきなりカメラが起動された状態になる。
②QRコードを読み取り時に処理が落ちる。
該当のソースコード
build.gradle(:app)
plugins { id 'com.android.application' } android { compileSdk 31 defaultConfig { applicationId "com.example.qrread" minSdk 24 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'com.journeyapps:zxing-android-embedded:4.1.0' }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/Qr_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="QRカメラ起動" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="parent" app:layout_constraintEnd_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.qrread; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.SpannableStringBuilder; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.Qr_button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new IntentIntegrator(MainActivity.this).initiateScan(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data); if(result.getContents() != null) { if (result.getContents() != null) { Toast.makeText(MainActivity.this, result.getContents(), Toast.LENGTH_LONG).show(); } } else { super.onActivityResult(requestCode, requestCode, data); } } }
補足情報(FW/ツールのバージョンなど)
・Android Studio:Android Studio Arctic Fox 2020.3.1 Patch 4
・パソコンの機種:LIFEBOOK AH53/U FMVA53UW
・OS のバージョン:Windows 10 Home バージョン 21H1
・CPU :"Core i7 4722HQ(Haswell)"
・GPU :"インテル® HD グラフィックス 4600"
デバック情報 入りきらなかったので一部抜粋
12/17 02:11:50: Launching 'app' on samsung SC-04J.
App restart successful without requiring a re-install.
$ adb shell am start -n "com.example.qrread/com.journeyapps.barcodescanner.CaptureActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.qrread | com.example.qrread.test
Waiting for application to come online: com.example.qrread | com.example.qrread.test
Connected to process 8243 on device 'samsung-sc_04j-521031c3508354b5'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.example.qrread is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
Connecting to com.example.qrread
Connected to the target VM, address: 'localhost:52373', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.example.qrread is waiting for the debugger on port 8100...
~中略~
D/CameraInstance: Camera is closed, not requesting preview
V/MediaPlayerNative: invoke 68
D/CameraPreview: pause()
D/CameraPreview: pause()
D/ViewRootImpl@884794c[CaptureActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@884794c[CaptureActivity]: MSG_WINDOW_FOCUS_CHANGED 0
V/MediaPlayerNative: message received msg=6, ext1=0, ext2=0
unrecognized message: (6, 0, 0)
callback application
back from callback
V/MediaPlayer-JNI: getCurrentPosition: 0 (msec)
V/MediaPlayerNative: message received msg=2, ext1=0, ext2=0
V/MediaPlayerNative: playback complete
callback application
V/MediaPlayerNative: back from callback
V/MediaPlayer-JNI: stop
V/MediaPlayerNative: stop
V/MediaPlayerNative: message received msg=8, ext1=0, ext2=0
unrecognized message: (8, 0, 0)
callback application
back from callback
V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer-JNI: release
V/MediaPlayerNative: setListener
disconnect
V/MediaPlayerNative: destructor
V/MediaPlayerNative: disconnect
W/MediaPlayer: mediaplayer went away with unhandled events
D/SurfaceView: BG show() Surface(name=Background for - SurfaceView - com.example.qrread/com.journeyapps.barcodescanner.CaptureActivity@3c17605@0) android.view.SurfaceView{3c17605 V.E...... ........ 0,0-720,1280}
surfaceDestroyed 1 #2 android.view.SurfaceView{3c17605 V.E...... ........ 0,0-720,1280}
D/SurfaceView: BG destroy() Surface(name=Background for - SurfaceView - com.example.qrread/com.journeyapps.barcodescanner.CaptureActivity@3c17605@0) android.view.SurfaceView{3c17605 V.E...... ........ 0,0-720,1280}
D/OpenGLRenderer: eglDestroySurface = 0xd36f59e0
D/ViewRootImpl@884794c[CaptureActivity]: Relayout returned: old=[0,0][720,1280] new=[0,0][720,1280] result=0x5 surface={valid=false 0} changed=true
D/InputTransport: Input channel destroyed: fd=73
D/ViewRootImpl@884794c[CaptureActivity]: dispatchDetachedFromWindow
D/InputEventReceiver: channel 'fac809c com.example.qrread/com.journeyapps.barcodescanner.CaptureActivity (client)' ~ Disposing input event receiver.
channel 'fac809c com.example.qrread/com.journeyapps.barcodescanner.CaptureActivity (client)' ~NativeInputEventReceiver.
D/InputTransport: Input channel destroyed: fd=58
V/MediaPlayer-JNI: native_finalize
release
ターゲット VM から切断されました。アドレス: 'localhost:52373', transport: 'socket'
回答1件
あなたの回答
tips
プレビュー