智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述Android studio错误:java.lang.NullPointerException [重复]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12个答案
主要信息
公共类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);
}
});
}
}
【Android studio错误(java.lang.NullPointerException [重复])】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() {@Overridepublic void onClick(View view) {AlertDialog.Builder mBuilder = new AlertDialog.Builder(selector.this);
mBuilder.setTitle("kjsr7c attendace");
mBuilder.setMultiChoiceItems(as, pora, new DialogInterface.OnMultiChoiceClickListener(){
@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {dialogInterface.dismiss();
}});
mBuilder.setNeutralButton("CLEAR", new DialogInterface.OnClickListener() {@Overridepublic 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应用程序类中有错误我正在检查auth!= null但仍然在if if语句中()
- 尝试调用虚方法'void android.view.View.setOnClickListener [duplicate]
- 在android [duplicate]中运行时检查权限时获取NullPointerException
- safari app extensions(从swift后台进程向所有选项卡广播消息)
- Android findViewById空指针[重复]
- 从Safari打开相机APP
- Java applet无法在Safari 7下打开文件(Mac OS X 10.9)
- Safari Webapp强制在单击其他链接时打开Safari
- 限制Google API的Android密钥使用