please help me with this as I have searched for solutions online without getting one.
What I am trying to do is get data from an alert dialog onclick the OK button. The alert dialog pops up after clicking an item on TypeActivity. The result of this click is passed to HomeActivity from where the alert dialog pops up with a text field. But each time I type in the text field and press the "OK" button, the app crashes. The logcat shows A null pointer Exception. I will display the logcat content too. Thanks for your help.
My codes are as follows
TypeActivity:
String[] typeTag = {"TypeA","TypeB","TypeC","TypeD"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyAdapter(TypeActivity.this,R.layout.spinnerrow, typeTag));
getListView().setAdapter(new MyAdapter(TypeActivity.this,R.layout.spinnerrow, typeTag));
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent returnIntent = new Intent();
returnIntent.putExtra("selectedType", typeTag[position]);
setResult(RESULT_OK, returnIntent);
finish();
}
});
HomeActivity:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK){
String temp = data.getStringExtra("selectedType");
AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
ViewGroup parent = null;
builder2.setView(inflater.inflate(R.layout.inputtype, parent, false));
final EditText tagname = (EditText)findViewById(R.id.uname);
builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id) {
String tag = tagname.getText().toString();
Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.show();
}
}
The Logcat:
1-11 05:22:45.427: E/AndroidRuntime(2298): FATAL EXCEPTION: main
11-11 05:22:45.427: E/AndroidRuntime(2298): Process: com.example.myapp, PID: 2298
11-11 05:22:45.427: E/AndroidRuntime(2298): java.lang.NullPointerException
11-11 05:22:45.427: E/AndroidRuntime(2298): at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:278)
11-11 05:22:45.427: E/AndroidRuntime(2298): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-11 05:22:45.427: E/AndroidRuntime(2298): at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 05:22:45.427: E/AndroidRuntime(2298): at android.os.Looper.loop(Looper.java:136)
11-11 05:22:45.427: E/AndroidRuntime(2298): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-11 05:22:45.427: E/AndroidRuntime(2298): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 05:22:45.427: E/AndroidRuntime(2298): at java.lang.reflect.Method.invoke(Method.java:515)
11-11 05:22:45.427: E/AndroidRuntime(2298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-11 05:22:45.427: E/AndroidRuntime(2298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-11 05:22:45.427: E/AndroidRuntime(2298): at dalvik.system.NativeStart.main(Native Method)
the new logcat
11-11 06:06:35.917: E/AndroidRuntime(4530): FATAL EXCEPTION: main
11-11 06:06:35.917: E/AndroidRuntime(4530): Process: com.example.myapp, PID: 4530
11-11 06:06:35.917: E/AndroidRuntime(4530): java.lang.NullPointerException
11-11 06:06:35.917: E/AndroidRuntime(4530): at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:282)
11-11 06:06:35.917: E/AndroidRuntime(4530): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-11 06:06:35.917: E/AndroidRuntime(4530): at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 06:06:35.917: E/AndroidRuntime(4530): at android.os.Looper.loop(Looper.java:136)
11-11 06:06:35.917: E/AndroidRuntime(4530): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-11 06:06:35.917: E/AndroidRuntime(4530): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 06:06:35.917: E/AndroidRuntime(4530): at java.lang.reflect.Method.invoke(Method.java:515)
11-11 06:06:35.917: E/AndroidRuntime(4530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-11 06:06:35.917: E/AndroidRuntime(4530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-11 06:06:35.917: E/AndroidRuntime(4530): at dalvik.system.NativeStart.main(Native Method)
The layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/dispimg"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#d1d1d1"
android:contentDescription="@string/app_name"
android:scaleType="center"
android:src="@drawable/addme" />
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="16dp"
android:inputType="text"
android:background="@drawable/textfield" />
</LinearLayout>
the new code:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK){
String temp = data.getStringExtra("selectedType");
AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
ViewGroup parent = null;
View view = inflater.inflate(R.layout.inputtype, parent, true);
final EditText tagname = (EditText)findViewById(R.id.uname);
builder2.setView(view);
builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id) {
String tag = tagname.getText().toString();
Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.show();
}
}
new logcat:
11-11 06:58:49.804: E/AndroidRuntime(7148): FATAL EXCEPTION: main
11-11 06:58:49.804: E/AndroidRuntime(7148): Process: com.example.myapp, PID: 7148
11-11 06:58:49.804: E/AndroidRuntime(7148): java.lang.NullPointerException
11-11 06:58:49.804: E/AndroidRuntime(7148): at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:288)
11-11 06:58:49.804: E/AndroidRuntime(7148): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-11 06:58:49.804: E/AndroidRuntime(7148): at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 06:58:49.804: E/AndroidRuntime(7148): at android.os.Looper.loop(Looper.java:136)
11-11 06:58:49.804: E/AndroidRuntime(7148): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-11 06:58:49.804: E/AndroidRuntime(7148): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 06:58:49.804: E/AndroidRuntime(7148): at java.lang.reflect.Method.invoke(Method.java:515)
11-11 06:58:49.804: E/AndroidRuntime(7148): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-11 06:58:49.804: E/AndroidRuntime(7148): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-11 06:58:49.804: E/AndroidRuntime(7148): at dalvik.system.NativeStart.main(Native Method)
your editText is belong to AlertDialog, so following line is null in your code:
final EditText tagname = (EditText)findViewById(R.id.uname);
so you get NPE on
String tag = tagname.getText().toString();
you need following code:
View view = inflater.inflate(R.layout.inputtype, parent, false)
builder2.setView(view);
and
final EditText tagname = (EditText)view.findViewById(R.id.uname);
so your code must be as following:
AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
ViewGroup parent = null;
View view = inflater.inflate(R.layout.inputtype, parent, false);
final EditText tagname = (EditText)view.findViewById(R.id.uname);
builder2.setView(view);
builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id) {
String tag = tagname.getText().toString();
Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.show();
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.