简体   繁体   中英

Null pointer exception in android

I have the following code for my dialog box, I want to get some text from the alert box, but the below given code throws null pointer exception.

public EditText summarytext;
public Button done;
Context mContext = getApplicationContext();
    Dialog dialog = new Dialog(mContext);
    dialog.setContentView(R.layout.summaryalert);
    dialog.setTitle("Other");

    done=(ButtonfindViewById(R.id.btn);
    done.setOnClickListener(don);<----Nullpointer Exception


     private View.OnClickListener don=new  View.OnClickListener() {
                 @Override          
public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(v==findViewById(R.id.btn)){
                    summarytext=(EditText)findViewById(R.id.summarytext);
                    sumry=summarytext.getText().toString();
                    String display=""+sumry;
                    Toast toast = Toast.makeText(getBaseContext(),
     display, Toast.LENGTH_LONG);
                    toast.show();
                    }


summary alert

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableRow
android:id="@+id/widget60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="@+id/text"
android:text="Other(provide summary)"
android:textColor="#ffffff"
></TextView></TableRow>
<TableRow
android:id="@+id/widget60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<EditText
android:id="@+id/summarytext" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/widget60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:id="@+id/btn"
android:text="Done"
></Button>
</TableRow>

mainlayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableLayout
android:id="@+id/widget58"
android:layout_width="320px"
android:layout_height="65px"
android:orientation="vertical"
>
<TableRow
android:id="@+id/widget60"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<EditText
android:id="@+id/other" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:ems="15">
</EditText>
</TableRow>
<TableRow
android:id="@+id/widget42"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
>
<Button
android:id="@+id/submit"
android:layout_width="155px"
android:layout_height="41px"
android:text="Done"
android:layout_x="77px"
android:layout_y="313px"
>
</Button>
</TableRow>>
</TableLayout>

Edit

solved..!

include 'dialog'

done=(Button)dialog.findViewById(R.id.btn);

Try using dialog.findViewById(r.id.summarytext) you should also probably be using this for your done button also. But you'll have to make the call for that after your dialog.show(). On a side note you can use getApplicationContext() for your Toast instead of getBaseContext().

findViewById(int) can return null if the specified id is not found in the layout.

http://developer.android.com/reference/android/app/Activity.html#findViewById(int )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM