我的代码存在问题
$(function() {
$('.reset').on('click',function () {
$(':checkbox').prop('checked',false);
$(':text').hide();
$('.applabel').show();
});
$('.edit').on('click',function () {
$('.ed-up').each(function(){
if($('.ed-up').prop('checked') == true)
alert($(this).next('.applabel').text());
});
});
});
问题是当我单击按钮Ok_action时出现此错误:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'。我认为问题出在public class AgendaActivity extends AppCompatActivity {
private DbManager db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agendalayout);
db=new DbManager(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addxml, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_add:
final Dialog d = new Dialog(this);
d.setTitle("Prova");
d.setCancelable(false);
d.setContentView(R.layout.popupdialog);
Window window = d.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.show();
//eventi Button
Button b = (Button) d.findViewById(R.id.ok_action);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText p1 = (EditText) findViewById(R.id.Ora_inizio);
EditText p2 = (EditText) findViewById(R.id.Ora_fine);
EditText p3 = (EditText) findViewById(R.id.pause);
String p1Value = p1.getText().toString();
String p2Value = p2.getText().toString();
String p3Value = p3.getText().toString();
try {
int p1Final = Integer.parseInt(p1Value);
} catch (Exception e) {
Toast exceptionAgenda = Toast.makeText(AgendaActivity.this,
"Uno dei due campi o entrambi sono vuoti!",
Toast.LENGTH_LONG);
exceptionAgenda.show();
}
//db.save(primoValore,secondoValore,terzoValore);
}
});
default:
}
return true;
};
}
}
});
Button b2=(Button) d.findViewById(R.id.canceldialog);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(d.getContext(), "cliccatocancel", Toast.LENGTH_LONG).show();
d.cancel();
}
});
行,我不知道如何解决。有人能帮助我吗?
添加输出错误:
String p1Value = p1.getText().toString();
XML
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: FATAL EXCEPTION: main
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: Process: com.apps.giak.lavorotime, PID: 16356
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.apps.giak.lavorotime.AgendaActivity$1.onClick(AgendaActivity.java:62)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View.performClick(View.java:5155)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20747)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5832)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteIn
`
答案 0 :(得分:2)
请使用
EditText p1 = (EditText) d.findViewById(R.id.Ora_inizio);
而不是
EditText p1 = (EditText) findViewById(R.id.Ora_inizio);
因为ID为R.id.Ora_inizio
的视图位于对话框中,即R.layout.popupdialog
。
所以你必须使用
Dialog.findViewById()
方法。
但现在您尝试从活动布局Edittext
而不是对话框布局中访问此(R.layout.agendalayout)
。
答案 1 :(得分:1)
你可以告诉 - 甚至没有查看你的源代码 - 问题是什么,只是错误信息和你说的那一行。 p1为空;并且您尝试在其上运行getText()
。 Null没有方法getText()
,因此您获得空指针异常。
findViewById(R.id.Ora_inizio)
可能返回null,这意味着Android无法在当前布局中通过该ID查找视图。仔细检查此ID是否用于当前布局中的视图。