0

カスタム オーバーレイ ビューをビューに追加したいのですがNullPointerException、ボタンを押してカスタムDialogEmailOverlayビューを追加すると が表示されます。

emailBtn.setOnClickListener(new View.OnClickListener() {
  def onClick(v: View) {
    println("test email")
    detailViewLayout.addView(dialogEmailOverlay)
  }
})

アクティビティ クラスは次のとおりです。

class BookDetailsActivity extends Activity with ActivityUtil {

  lazy val book_detail_title = find[TextView](R.id.book_detail_title)
  lazy val book_detail_authors = find[TextView](R.id.book_detail_authors)
  lazy val book_detail_publisher = find[TextView](R.id.book_detail_publisher)
  lazy val book_detail_description = find[TextView](R.id.book_detail_description)
  lazy val detailViewLayout = find[RelativeLayout](R.layout.activity_book_details)
  lazy val emailBtn = findViewById(R.id.email).asInstanceOf[Button]

  lazy val dialogEmailOverlay = LayoutInflater.from(this).inflate(R.layout.dialog_email_overlay,
    detailViewLayout, false).asInstanceOf[DialogEmailOverlay]


  protected val imageLoader = ImageLoader.getInstance

  lazy val options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub)
    .showImageForEmptyUri(R.drawable.ic_empty)
    .showImageOnFail(R.drawable.ic_error)
    .cacheInMemory()
    .build()

  def getString = getResources.getString _

  protected override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_book_details)
    setupActionBar()

    val bundle = getIntent.getExtras
    val book = bundle.getSerializable("book").asInstanceOf[BookMetadata]

    book_detail_title.setText(book.title)
    book_detail_authors.setText(getString(R.string.author_by) + " " + book.authors)
    book_detail_publisher.setText(getString(R.string.publisher) + " " + book.publisher.name)
    imageLoader.displayImage(BitLitAPI.baseUrl + book.cover, find[ImageView](R.id.book_detail_cover_image), options)
    book_detail_description.setText(if (book.description != null) book.description else "")

    emailBtn.setOnClickListener(new View.OnClickListener() {

      def onClick(v: View) {
        println("test email")
        detailViewLayout.addView(dialogEmailOverlay)

      }
    })

    dialogEmailOverlay onNoBtn {
      finish()
    }

    dialogEmailOverlay onYesBtn {
      println("email send")
    }
  }

  private def setupActionBar() {
    getActionBar.setDisplayHomeAsUpEnabled(true)
  }

  override def onCreateOptionsMenu(menu: Menu): Boolean = {
    getMenuInflater.inflate(R.menu.book_details, menu)

    true
  }
}

私のログ:

I/System.out(19028): test email
D/AndroidRuntime(19028): Shutting down VM W/dalvikvm(19028): threadid=1: thread exiting with uncaught exception (group=0x41505700)
E/AndroidRuntime(19028): FATAL EXCEPTION: main
E/AndroidRuntime(19028): java.lang.NullPointerException
E/AndroidRuntime(19028):    at com.test.android.BookDetailsActivity$$anon$1.onClick(BookDetailsActivity.scala:59)
E/AndroidRuntime(19028):    at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(19028):    at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(19028):    at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(19028):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(19028):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(19028):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(19028):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19028):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(19028):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(19028):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(19028):    at dalvik.system.NativeStart.main(Native Method)

そしてDialogEmailOverlayクラス:

class DialogEmailOverlay(context: Context, attrs: AttributeSet, defStyle: Int) extends RelativeLayout(context, attrs, defStyle) {

  def this(context: Context, attrs: AttributeSet) {
    this(context, attrs, 0)
  }

  lazy val title = findViewById(R.id.dialog_email_title).asInstanceOf[TextView]
  lazy val buttonNo = findViewById(R.id.dialog_email_button_no).asInstanceOf[Button]
  lazy val buttonYes = findViewById(R.id.dialog_email_button_yes).asInstanceOf[Button]

  def showBook(book: BookMetadata) {
    title.setText(book.title)
  }

  def onNoBtn[T](f: => T) {
    buttonNo.setOnClickListener(new View.OnClickListener() {
      override def onClick(view: View) = f
    })
  }

  def onYesBtn[T](f: => T) {
    buttonYes.setOnClickListener(new View.OnClickListener() {
      override def onClick(view: View) = f 
      })
  }
}
4

1 に答える 1

1

リンクで提供したコードから:

lazy val detailViewLayout = find[RelativeLayout](R.layout.activity_book_details)

R.layout.activity_book_details間違っています。

detailViewLayoutに初期化されnullます。したがって、NPE は次のとおりです。

detailViewLayout.addView(dialogEmailOverlay)

RelativeLayout中に がある場合はR.layout.activity_book_details、 を付けますid。この ID を使用して検索します。

lazy val detailViewLayout = find[RelativeLayout](R.id.relative_layout_id)
于 2013-09-09T19:56:37.847 に答える