4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カスタムダイアログでのfindViewByIDでnullが返ってくる

Last updated at Posted at 2016-08-10

次のようなカスタムダイアログを作ろうと思っていたのですが,右上の「ログ書き出し」を押した後このダイアログが出る前に落ちてしまいました.
s_Screenshot_20160810-091950[1].png
他の人も書いているかもしれませんが忘れないように書いておきます.
writeLog()というメソッドがカスタムダイアログを使っています.

MainActivity.java
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
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.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button resetLogButton;
    private Button writeLogButton;
    private TextView logTextView;

    private int counter = 0;
    private String log = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resetLogButton = (Button) findViewById(R.id.resetLogButton);
        writeLogButton = (Button) findViewById(R.id.writeLogButton);
        logTextView = (TextView) findViewById(R.id.logTextView);
        resetLogButton.setOnClickListener(this);
        writeLogButton.setOnClickListener(this);

        for (int i = 0; i < 100; i++) {
            addLog("Log Message " + i);
        }
    }

    public void addLog(String str) {
        log += counter + " " + str + "\n";
        logTextView.setText(log);
        counter++;
    }

    @Override
    public void onClick(View v) {
        if (v == resetLogButton) {
            counter = 0;
            log = "";
            logTextView.setText(log);
        } else if (v == writeLogButton) {
            writeLog();
        }
    }

    private void writeLog() {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        final View dialogLayout = inflater.inflate(R.layout.write_log_dialog, (ViewGroup) findViewById(R.id.layout_root));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("書き出すログを指定");
        //builder.setMessage("");
        builder.setView(dialogLayout);
        builder.setPositiveButton("書き出し", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                RadioGroup radioGroup = (RadioGroup) dialogLayout.findViewById(R.id.writeLogRadioGroup);
                if (radioGroup.getCheckedRadioButtonId() == R.id.applicationLogRadioButton) {
                    //write applicationLog
                    Log.i("dialog","application log");
                } else {
                    //write systemLog
                    Log.i("dialog","system log");
                }
            }
        });

        builder.setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }
        );

        builder.create().show();
    }
}

そして次がカスタムダイアログのレイアウトです.

write_log_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <RadioGroup
        android:id="@+id/writeLogRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/applicationLogRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="アプリケーションのログ" />

        <RadioButton
            android:id="@+id/systemLogRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="システムログ" />
    </RadioGroup>
</LinearLayout>

落ちていた原因は

RadioGroup radioGroup = (RadioGroup) dialogLayout.findViewById(R.id.writeLogRadioGroup);

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.writeLogRadioGroup);

と書いていたことでした.
同時にMainActivityとカスタムダイアログで2つのレイアウトを操作しているので,どっちのViewに属しているIDなのかを明示的に示さなかったから,見つけることができなくてnullが返ってきていたんですね.

追記:
Nkznさんご指摘ありがとうございます.
しっかりと理解したい方はNkznさんのコメントを見てください.

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?