0

I am pretty new to Android. I have the following code to display a Custom Dialog when the user clicks on a button. Here is the code:

MainActivity.java

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

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

    Button customBtn = (Button)findViewById(R.id.custombtn);
    customBtn.setOnClickListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v)
{
    if(v == findViewById(R.id.custombtn))
    {
        String message = "This is a Custom Dialog.";

        Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.custom_layout);
        dialog.setTitle("Custom Dialog");

        TextView texter = (TextView)findViewById(R.id.text);
        texter.setText(message);

        ImageView image = (ImageView)findViewById(R.id.image);
        //image.setImageResource(R.drawable.abc_spinner_mtrl_am_alpha);
        dialog.show();
    }
}
}

And here is the custom_layout.xml file

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="@color/accent_material_light"/>
</LinearLayout>

WHenever i run the app, and click on the Custom Button, the app Crashes with the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.adhish.onclicklisten.MainActivity.onClick(MainActivity.java:105)

I am pretty new to Android and don't know what this means. I have tried Googling and it doesn't work in my case.

Please help.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
Adhish Thite
  • 463
  • 2
  • 5
  • 20

1 Answers1

1

Use dialog to access views from custom_layout layout :

    TextView texter = (TextView)dialog.findViewById(R.id.text);
    texter.setText(message);
    ImageView image = (ImageView)dialog.findViewById(R.id.image); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213