Android studio错误:java.lang.NullPointerException [重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android studio错误:java.lang.NullPointerException [重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我正在尝试从用户那里获取输入并创建给定数字的列表。我收到java.lang.NULLpointerException但我在代码中找不到任何错误。将用户输入从MainActivity传递到选择器Activity。主要活动完美无缺。
主要信息
公共类MainActivity扩展AppCompatActivity {
DatePicker dp;
EditText et;
Button b;
String d;
int e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dp = (DatePicker)findViewById(R.id.datePicker3);
et = (EditText)findViewById(R.id.editText);
b = (Button)findViewById(R.id.button);
d = dp.toString();
//final int k = Integer.parseInt(e);
//Integer.
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, selector.class);
Bundle extras = new Bundle();
e = Integer.parseInt(et.getText().toString());
i.putExtra("date",d);
i.putExtra("number", e);
i.putExtras(extras);
startActivity(i);
}
});
}
}
Selector Acitivity公共类选择器扩展AppCompatActivity {
Button b;
TextView abs;
String []as;
boolean[] pora;
ArrayList<Integer> mabs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selector);
b=(Button)findViewById(R.id.btnOrder);
abs=(TextView)findViewById(R.id.tvItemSelected);
Bundle extras = getIntent().getExtras();
int result = extras.getInt("number",5);
String v2 =Integer.toString(result);
abs.setText(v2);
as = new String[result+1];
for(int i=1;i<=result;i++)
{
as[i]=Integer.toString(i);
}
abs.append(as[2]);
pora=new boolean[as.length];
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(selector.this);
mBuilder.setTitle("kjsr7c attendace");
mBuilder.setMultiChoiceItems(as, pora, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
if(isChecked){
mabs.add(position);
}else{
mabs.remove((Integer.valueOf(position)));
}
}
});
mBuilder.setCancelable(false);
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
String item = "";
for (int i = 1; i < mabs.size(); i++) {
item = item + as[mabs.get(i)];
if (i != mabs.size() - 1) {
item = item + ", ";
}
}
abs.setText(item);
}
});
mBuilder.setNegativeButton("DISMISS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setNeutralButton("CLEAR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
for (int i = 1; i < pora.length; i++) {
pora[i] = false;
mabs.clear();
abs.setText("");
}
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
}
});
}
}
logcat的
FATAL EXCEPTION: main
Process: com.example.sankar.kjsr7c, PID: 3945
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
at android.support.v7.app.AlertController$AlertParams$1.getView(AlertController.java:974)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
at android.widget.ListView.onMeasure(ListView.java:1233)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.support.v7.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:133)
at android.support.v7.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:63)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1339)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案
在使用Integer.parseInt之前检查EditText是否为空。最有可能的是,Empty EditText将为getText()操作返回null。
以上是关于Android studio错误:java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章