我正在使用android。现在我遇到了问题。我创建了一个程序,它将通过警告对话框从用户获取字符串。并将其添加到微调器中。但它在logcat中显示了一些例外。请任何人帮我解决这个问题。这是我的代码
**MainActivity.java**
package com.example.picker;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
Button add_button;
Editable value;
String inserted_value;
AlertDialog.Builder alert;
Spinner spinner;
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_button = (Button)findViewById(R.id.addbutton);
alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
/** Getting a reference to Spinner object of the resource activity_main */
spinner = (Spinner) findViewById(R.id.spin1);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alert.setTitle("Picker");
alert.setMessage("Enter a string...");
// Set an EditText view to get user input
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(inserted_value !=null){
Log.d("GOT_TEXT",inserted_value);
list.add(input.getText().toString());
}
else{
Log.d("GOT_TEXT","inserted_value is null");
}
/** Setting the adapter to the ListView */
spinner.setAdapter(adapter);
/** Adding radio buttons for the spinner items */
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.dismiss();
}
});
alert.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
logcat的
01-28 13:45:56.138: E/AndroidRuntime(701): FATAL EXCEPTION: main
01-28 13:45:56.138: E/AndroidRuntime(701): java.lang.NullPointerException
01-28 13:45:56.138: E/AndroidRuntime(701): at com.example.picker.MainActivity$1$1.onClick(MainActivity.java:76)
01-28 13:45:56.138: E/AndroidRuntime(701): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
01-28 13:45:56.138: E/AndroidRuntime(701): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 13:45:56.138: E/AndroidRuntime(701): at android.os.Looper.loop(Looper.java:123)
01-28 13:45:56.138: E/AndroidRuntime(701): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-28 13:45:56.138: E/AndroidRuntime(701): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 13:45:56.138: E/AndroidRuntime(701): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 13:45:56.138: E/AndroidRuntime(701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-28 13:45:56.138: E/AndroidRuntime(701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-28 13:45:56.138: E/AndroidRuntime(701): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
尝试:
if(inserted_value !=null){
Log.d("GOT_TEXT",inserted_value);
}
else{
Log.d("GOT_TEXT","inserted_value is null");
}
因为当您尝试登录时inserted_value
为NULL
答案 1 :(得分:1)
试试这个,
if(input.getText().length() != 0)
{
list.add(input.getText().toString());
}else{
}