在主要活动我有一个按钮为添加一个新的注释(这是一个类型的即时贴android应用):
ArrayAdapter<注意>适配器=(ArrayAdapter<注意>)getListAdapter(); 注意注意= NULL; 开关(view.getId()){ 案例R.id.add: //调用AddActivity指定音符领域的内容要 //添加到数据库 intentToAdd =新意图(这一点,AddActivity.class); startActivity(intentToAdd); //从AddActivity群众演员 注意newNote = getIntent()getExtras()getParcelable(myNewNote)。。 字符串摘要= newNote.getSummary(); 字符串的详细信息= newNote.getDetails(); //保存新的注释在数据库 注= datasource.createNote(概要,详细说明); adapter.add(注); 打破;
然后我在 AddActivity (其中包含一个表格添加新的注),它的 onCreate方法是:
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_add); butAdd =(按钮)findViewById(R.id.buttonAjout); intentToMain =新意图(这一点,MainActivity.class); butAdd.setOnClickListener(新OnClickListener(){ @覆盖 公共无效的onClick(视图v){ // TODO自动生成方法存根 总结的EditText =((的EditText)findViewById(R.id.editTextSum)); 细节的EditText =((的EditText)findViewById(R.id.editTextDet)); 注意注意=新的注(summary.toString(),details.toString()); intentToMain.putExtra(myNewNote注); startActivity(intentToMain); } });}
N.B:我的笔记类实现Parcelable类
下面是我的问题:当我点击添加按钮中的主要活动我的应用程序崩溃(意外停止),为什么:( ???
告诉我,如果有一些其他的信息缺失或code ...谢谢...
在LogCat中说,这是一个NullPointerException异常:
04-21 17:00:07.832:D / AndroidRuntime(278):关闭VM04-21 17:00:07.832:W / dalvikvm(278):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)04-21 17:00:07.892:E / AndroidRuntime(278):致命异常:主要04-21 17:00:07.892:E / AndroidRuntime(278):java.lang.IllegalStateException:无法执行活动的方法04-21 17:00:07.892:E / AndroidRuntime(278):在android.view.View $ 1.onClick(View.java:2072)04-21 17:00:07.892:E / AndroidRuntime(278):在android.view.View.performClick(View.java:2408)04-21 17:00:07.892:E / AndroidRuntime(278):在android.view.View $ PerformClick.run(View.java:8816)04-21 17:00:07.892:E / AndroidRuntime(278):在android.os.Handler.handleCallback(Handler.java:587)04-21 17:00:07.892:E / AndroidRuntime(278):在android.os.Handler.dispatchMessage(Handler.java:92)04-21 17:00:07.892:E / AndroidRuntime(278):在android.os.Looper.loop(Looper.java:123)04-21 17:00:07.892:E / AndroidRuntime(278):在android.app.ActivityThread.main(ActivityThread.java:4627)04-21 17:00:07.892:E / AndroidRuntime(278):在java.lang.reflect.Method.invokeNative(本机方法)04-21 17:00:07.892:E / AndroidRuntime(278):在java.lang.reflect.Method.invoke(Method.java:521)04-21 17:00:07.892:E / AndroidRuntime(278):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)04-21 17:00:07.892:E / AndroidRuntime(278):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)04-21 17:00:07.892:E / AndroidRuntime(278):在dalvik.system.NativeStart.main(本机方法)04-21 17:00:07.892:E / AndroidRuntime(278):由:java.lang.reflect.InvocationTargetException04-21 17:00:07.892:E / AndroidRuntime(278):在com.insat.notememoiresqlite.MainActivity.onClick(MainActivity.java:69)04-21 17:00:07.892:E / AndroidRuntime(278):在java.lang.reflect.Method.invokeNative(本机方法)04-21 17:00:07.892:E / AndroidRuntime(278):在java.lang.reflect.Method.invoke(Method.java:521)04-21 17:00:07.892:E / AndroidRuntime(278):在android.view.View $ 1.onClick(View.java:2067)04-21 17:00:07.892:E / AndroidRuntime(278):11 ...更多04-21 17:00:07.892:E / AndroidRuntime(278):致:显示java.lang.NullPointerException04-21 17:00:07.892:E / AndroidRuntime(278):15 ...更多04-21 17:04:37.812:I /流程(278):发送信号。 PID:278 SIG:9
我想这是因为在主要活动这些行:
字符串摘要= newNote.getSummary();字符串的详细信息= newNote.getDetails();
在我尝试从newNote得到的值,而它的空但是为什么空?
-----------------------------------------------------------Edit-----------------------------------------------------------
我试过的onActivityResult的解决方案,但我仍然有这一行一个NullPointerException异常:
注意newNote = getIntent()getExtras()getParcelable(myNewNote)。;
这里是code:在主要活动:
公共无效的onClick(查看视图){ 适配器=(ArrayAdapter<注意>)getListAdapter(); 开关(view.getId()){ 案例R.id.add: //调用AddActivity指定音符领域的内容要 //添加到数据库 intentToAdd =新意图(这一点,AddActivity.class); startActivityForResult(intentToAdd,0); 打破;
,然后:
@覆盖 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){ // TODO自动生成方法存根 Log.i(AAAAA,丹斯乐的onActivityResult); 如果(结果code == RESULT_OK){ Log.i(AAAAA,RESULT_OK); //从AddActivity群众演员 注意newNote = getIntent()getExtras()getParcelable(myNewNote)。。 如果(newNote!= NULL){ 字符串摘要= newNote.getSummary(); 字符串的详细信息= newNote.getDetails(); //保存新的注释在数据库 注= datasource.createNote(概要,详细说明); adapter.add(注); }其他{ Log.i(AAAAA,newNote EST空!!!!!!!!); } }其他{ Log.i(AAAAA,RESULT_PAS OK); } }
,并在AddActivity:
super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_add); Log.i(AAAAA,丹斯乐德的onCreate AddActivity); butAdd =(按钮)findViewById(R.id.buttonAjout); intentToMain =新意图(这一点,MainActivity.class); butAdd.setOnClickListener(新OnClickListener(){ @覆盖 公共无效的onClick(视图v){ // TODO自动生成方法存根 Log.i(AAAAA,丹斯乐杜的onClick溥敦ajout); 总结的EditText =((的EditText)findViewById(R.id.editTextSum)); 细节的EditText =((的EditText)findViewById(R.id.editTextDet)); 注意注意=新的注(summary.toString(),details.toString()); intentToMain.putExtra(myNewNote注); 的setResult(RESULT_OK); 完(); } });
解决方案
您也可以尝试这个...
请持有记实例的静态类,然后在AddActivity值添加到为
StaticClass.noteitem.summary =ABC; StaticClass.noteitem.details =ABCDEF;
然后在你的mainactivity支票OnREsume
如果(StaticClass.noteitem!= NULL)
和访问您的项目作为StaticClass.noteitem.summary等。
In the Main activity I have a button to add a new Note (it's a sort of sticky notes android application):
ArrayAdapter<Note> adapter = (ArrayAdapter<Note>) getListAdapter();
Note note = null;
switch (view.getId()) {
case R.id.add:
// Call AddActivity to specify content of the note fields to be
// added to the DB
intentToAdd = new Intent(this, AddActivity.class);
startActivity(intentToAdd);
//get the extras from AddActivity
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
String summary = newNote.getSummary();
String details = newNote.getDetails();
// Save the new note in the database
note = datasource.createNote(summary, details);
adapter.add(note);
break;
Then I have the AddActivity (which contains a form to add a new Note), its onCreate method is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
butAdd = (Button) findViewById(R.id.buttonAjout);
intentToMain = new Intent(this, MainActivity.class);
butAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText summary = ((EditText) findViewById(R.id.editTextSum));
EditText details = ((EditText) findViewById(R.id.editTextDet));
Note note =new Note(summary.toString(),details.toString());
intentToMain.putExtra("myNewNote", note);
startActivity(intentToMain);
}
});
}
N.B : My Note Class implements Parcelable Class !
Here's my problem: when I click on the add button in the main activity my application crashes ("stops unexpectedly") WHY :( ???
Tell me if there is some other missing information or code... Thank you ...
The logCat says it's a nullPointerException:
04-21 17:00:07.832: D/AndroidRuntime(278): Shutting down VM
04-21 17:00:07.832: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-21 17:00:07.892: E/AndroidRuntime(278): FATAL EXCEPTION: main
04-21 17:00:07.892: E/AndroidRuntime(278): java.lang.IllegalStateException: Could not execute method of the activity
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$1.onClick(View.java:2072)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View.performClick(View.java:2408)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$PerformClick.run(View.java:8816)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Handler.handleCallback(Handler.java:587)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 17:00:07.892: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 17:00:07.892: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 17:00:07.892: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): Caused by: java.lang.reflect.InvocationTargetException
04-21 17:00:07.892: E/AndroidRuntime(278): at com.insat.notememoiresqlite.MainActivity.onClick(MainActivity.java:69)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 17:00:07.892: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 17:00:07.892: E/AndroidRuntime(278): at android.view.View$1.onClick(View.java:2067)
04-21 17:00:07.892: E/AndroidRuntime(278): ... 11 more
04-21 17:00:07.892: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
04-21 17:00:07.892: E/AndroidRuntime(278): ... 15 more
04-21 17:04:37.812: I/Process(278): Sending signal. PID: 278 SIG: 9
I think it's because of these lines in the Main Activity:
String summary = newNote.getSummary();
String details = newNote.getDetails();
where I try to get values from newNote while it's null But why is it null?
-----------------------------------------------------------Edit-----------------------------------------------------------
I've tried the onActivityResult solution, but i still have a NullPointerException for this line:
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
Here is the code: In Main Activity:
public void onClick(View view) {
adapter = (ArrayAdapter<Note>) getListAdapter();
switch (view.getId()) {
case R.id.add:
// Call AddActivity to specify content of the note fields to be
// added to the DB
intentToAdd = new Intent(this, AddActivity.class);
startActivityForResult(intentToAdd, 0);
break;
then:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.i("aaaaa", "Dans le onActivityResult ");
if (resultCode == RESULT_OK) {
Log.i("aaaaa", "RESULT_OK ");
// get the extras from AddActivity
Note newNote = getIntent().getExtras().getParcelable("myNewNote");
if (newNote != null) {
String summary = newNote.getSummary();
String details = newNote.getDetails();
// Save the new note in the database
note = datasource.createNote(summary, details);
adapter.add(note);
} else {
Log.i("aaaaa", "newNote est null !!!!!!!! ");
}
} else {
Log.i("aaaaa", "RESULT_PAS OK ");
}
}
and in the AddActivity:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Log.i("aaaaa", "Dans le onCreate de AddActivity");
butAdd = (Button) findViewById(R.id.buttonAjout);
intentToMain = new Intent(this, MainActivity.class);
butAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("aaaaa", "Dans le onClick du bouton ajout");
EditText summary = ((EditText) findViewById(R.id.editTextSum));
EditText details = ((EditText) findViewById(R.id.editTextDet));
Note note =new Note(summary.toString(),details.toString());
intentToMain.putExtra("myNewNote", note);
setResult(RESULT_OK);
finish();
}
});
解决方案
You can also try this...
Make a static class which holds an instance of note,then in AddActivity add the values to it as
StaticClass.noteitem.summary="abc";
StaticClass.noteitem.details="abcdef";
then on the OnREsume of your mainactivity check
if (StaticClass.noteitem!=null)
and access your item as StaticClass.noteitem.summary etc.
上一篇:网址上做Laravel要求上做、网址、Laravel
下一篇:OpenCV的写作OpenCV的写作之间的差异为Android(移动设备)和桌面应用程序(OpenCV的2.4.3 usingC ++)应用程序、差异、桌面、设备