Kotlinでパッケージ名からアイコン(Drawable)とアプリ名を取得する処理を行いたいのですが、NullPointerExceptionが発生してしまいます。
以下にプログラムの全文を載せます。
Loading.ktでデータ(パッケージ名+その他)を読み込み、IntentでMainActivityに渡しています。MainActivityにてパッケージ名からアイコンとアプリ名を生産しています。(←ここでヌルポが発生 MainActivity.kt GetAppName(),GetAppIcon())
エラーメッセージは以下のとおりです。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
nullオブジェクトと言われても、宣言と同時にgetgetApplicationContext().getPackageManager()
で初期化しているはずなので、エラーメッセージの内容がよくわかりません...
ご教授お願いします。
※ヤフー知恵袋にてマルチポストを行っています。https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13210123708
MainActivity.kt
package caios.android.notification_log
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.File
class MainActivity : AppCompatActivity() {
//private var NotificationName: ArrayList<String>? = arrayListOf()
//private var NotificationText: ArrayList<String>? = arrayListOf()
//private var NotificationIcon: ArrayList<Drawable>? = arrayListOf()
private var BootNum = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getPackageManager()
if(BootNum == 0) {
Load()
BootNum++
val intent = getIntent()
if (intent != null) {
Log.d("System","Get Intent")
val NotificationName = intent.getStringArrayListExtra("Name")
val NotificationText = intent.getStringArrayListExtra("Text")
var FileCount = intent.getIntExtra("FileCount", 0)
if(FileCount >= 0) {
val NotificationLogList = MutableList(FileCount) { i ->
NotificationData(
NotificationName[i],
NotificationText[i]
)
}
val adapter = NotificationAdapter(this, NotificationLogList)
NotificationListView.adapter = adapter
Log.d("System","Set Adapter")
}
}
}
}
fun Load() {
Log.d("System","Call Load")
val intent = Intent(this, Loading::class.java)
startActivity(intent)
}
fun GetAppName(packageName: String): String {
Log.d("System","Call GetAppName")
var vPackageManager = getApplicationContext().getPackageManager() //例外
var apps = vPackageManager.getInstalledApplications(0)
for (ai in apps) {
if (packageName == ai.packageName) return ai.loadLabel(vPackageManager).toString()
}
return "error"
}
fun GetAppIcon(packageName: String): Drawable {
Log.d("System","Call GetAppIcon")
var vPackageManager = getApplicationContext().getPackageManager() //例外
return vPackageManager.getApplicationIcon(packageName)
}
}
data class NotificationData(val Title: String, val Text: String)
//data class NotificationViewHolder(var AppIconView: ImageView, var titleView: TextView, var textView: TextView)
class NotificationAdapter(context: Context, LogList:List<NotificationData>):ArrayAdapter<NotificationData>(context, 0, LogList) {
private val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
Log.d("System","getView")
val view = layoutInflater.inflate(R.layout.loglist,parent,false)
val AppIconView = view.findViewById<ImageView>(R.id.AppIconView)
val titleView=view.findViewById<TextView>(R.id.titleView)
val textView=view.findViewById<TextView>(R.id.textView)
val main = MainActivity()
Log.d("System","Set Data")
AppIconView.setImageDrawable(main.GetAppIcon(super.getItem(position).Title))
titleView.text = super.getItem(position).Title
textView.text = main.GetAppName(super.getItem(position).Text)
return view
}
}
Loading.kt
package caios.android.notification_log
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_loading.*
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.File
import java.io.Serializable
import kotlin.concurrent.thread
class Loading : AppCompatActivity() {
private var NotificationName: ArrayList<String> = arrayListOf()
private var NotificationText: ArrayList<String> = arrayListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_loading)
Log.d("System","Call Loading.kt")
thread {
CreateList()
}
}
fun CreateList() {
var inputStream = File(filesDir, "LogDateCount.txt")
if (inputStream.exists()) {
var FileCount = inputStream.bufferedReader().use(BufferedReader::readText)
var i: Int = FileCount.toInt() - 1
progressMark.visibility = android.widget.ProgressBar.VISIBLE
progressBar.max = FileCount.toInt()
Log.d("System","Count = $i")
while (i > 0) {
val FileName = "LogDate" + i + ".txt"
inputStream = File(filesDir, FileName)
Log.d("System","Open $FileName")
if (!inputStream.exists()) {
Toast.makeText(this, "ERROR:指定されたファイルが見つかりません $FileName", Toast.LENGTH_SHORT).show()
} else {
val TempText = inputStream.bufferedReader().use(BufferedReader::readText)
val array = TempText.split("\n")
NotificationName.add(array[0])
NotificationText.add(array[1])
Log.d("System","Name = ${array[0]} Text = ${array[1]}")
progressBar.progress = FileCount.toInt() - i
LoadText.text = (FileCount.toInt() - i).toString() + " / " + FileCount
}
i--;
}
Log.d("System","Load loop break")
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("Name", NotificationName)
intent.putExtra("Text", NotificationText)
intent.putExtra("FileCount", FileCount.toInt() - 1)
Log.d("System","Set Intent")
startActivity(intent)
Log.d("System","Start MainActivity")
} else {
finish()
Log.d("System","finish")
}
}
}