私には2つのクラスがあります。これらの名前はCoursesとMainActivityです。MainActivityにはLAUNCHERアクティビティがあり、Gridlayoutがあります。コースにはデフォルトのアクティビティがあります。GridLayoutをクリックすると、コースインターフェイスが開き、ユーザーがコースプログラムに従ってビューを入力しているため、コースオブジェクトが作成されます。このオブジェクトをMainActivityクラスに送信したいと思います。このオブジェクトを送信すると、 NullPointerExceptionを取得しています。この問題を解決しようとしましたが、解決できません。専門家の助けが必要です。
これがCourses.Javaです
package com.example.classprogram;
public class Courses extends Activity implements Parcelable {
private String course_name;
private String classroom;
private String teacname;
private String day;
private String start_hour;
private String end_hour;
List<String> days;
List<String> StartingHours;
public static String COURSENAME;
public static String CLASSROOM;
public static String TCHRNAME;
public static String DAY;
public static String SHOUR;
public static String EHOUR;
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public String getClassroom() {
return classroom;
}
public void setClassroom(String classroom) {
this.classroom = classroom;
}
public String getTeacname() {
return teacname;
}
public void setTeacname(String teacname) {
this.teacname = teacname;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getStart_hour() {
return start_hour;
}
public void setStart_hour(String start_hour) {
this.start_hour = start_hour;
}
public String getEnd_hour() {
return end_hour;
}
public void setEnd_hour(String end_hour) {
this.end_hour = end_hour;
}
Button add_class;
EditText cName;
EditText cRoom;
EditText tName;
Spinner gun;
Spinner sHour;
Spinner eHour;
public Courses() {}
public Courses(String course_name, String classroom, String teacname,
String day, String start_hour, String end_hour) {
super();
this.course_name = course_name;
this.classroom = classroom;
this.teacname = teacname;
this.day = day;
this.start_hour = start_hour;
this.end_hour = end_hour;
}
public Courses(Parcel in) {
/*String[] data = new String[5];
in.readStringArray(data);
this.course_name = data[0];
this.classroom = data[1];
this.teacname = data[2];
this.day = data[3];
this.end_hour = data[4];
this.start_hour = data[5];*/
course_name = in.readString();
classroom = in.readString();
teacname = in.readString();
day = in.readString();
start_hour = in.readString();
end_hour = in.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
//dest.writeStringArray(new String[]{this.course_name,this.classroom,this.teacname,this.day,this.start_hour,this.end_hour});
dest.writeString(course_name);
dest.writeString(classroom);
dest.writeString(teacname);
dest.writeString(day);
dest.writeString(start_hour);
dest.writeString(end_hour);
}
public static final Parcelable.Creator<Courses> CREATOR =new
Parcelable.Creator<Courses>() {
public Courses createFromParcel(Parcel in){
return new Courses(in);
}
public Courses[] newArray(int size){
return new Courses[size];
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_lecture);
final Courses course = new Courses();
add_class = (Button)findViewById(R.id.add);
cName = (EditText)findViewById(R.id.class_Name);
cRoom = (EditText)findViewById(R.id.classroom);
tName = (EditText)findViewById(R.id.teacher_name);
gun = (Spinner)findViewById(R.id.day);
sHour = (Spinner)findViewById(R.id.start_hour);
eHour = (Spinner)findViewById(R.id.end_hour);
days = new ArrayList<String>();
days.add("Monday");
days.add("Thuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
ArrayAdapter<String> adp = new ArrayAdapter<String>
(this, Android.R.layout.simple_dropdown_item_1line, days);
adp.setDropDownViewResource(Android.R.layout.simple_dropdown_item_1line);
gun.setAdapter(adp);
gun.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Courses a = new Courses();
switch(arg2) {
case 0 :
course.setDay("Monday");
break;
case 1 :
course.setDay("Tuesday");
break;
case 2 :
course.setDay("Wednesday");
break;
case 3 :
course.setDay("Thursday");
break;
case 4 :
course.setDay("Friday");
break;
case 5:
course.setDay("Saturday");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String[] Hours = getResources().getStringArray(R.array.Starting_Hours);
ArrayAdapter<String> adp1=new ArrayAdapter<String>
(this, Android.R.layout.simple_spinner_item,Hours);
adp1.setDropDownViewResource(Android.R.layout.simple_spinner_dropdown_item);
sHour.setAdapter(adp1);
sHour.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//Courses a = new Courses();
switch(arg2) {
case 0 :
course.setStart_hour("8:00");
break;
case 1 :
course.setStart_hour("8:30");
break;
case 2 :
course.setStart_hour("9:00");
break;
case 3 :
course.setStart_hour("9:30");
break;
case 4 :
course.setStart_hour("10:00");
break;
case 5:
course.setStart_hour("10:30");
break;
case 6:
course.setStart_hour("11:00");
break;
case 7:
course.setStart_hour("11:30");
break;
case 8:
course.setStart_hour("12:00");
break;
case 9:
course.setStart_hour("12:30");
break;
case 10:
course.setStart_hour("13:00");
break;
case 11:
course.setStart_hour("13:30");
break;
case 12:
course.setStart_hour("14:00");
break;
case 13:
course.setStart_hour("14:30");
break;
case 14:
course.setStart_hour("15:00");
break;
case 15:
course.setStart_hour("15:30");
break;
case 16:
course.setStart_hour("16:00");
break;
case 17:
course.setStart_hour("16:30");
break;
case 18:
course.setStart_hour("17:00");
break;
case 19:
course.setStart_hour("17:30");
break;
case 20:
course.setStart_hour("18:00");
break;
case 21:
course.setStart_hour("18:30");
break;
case 22:
course.setStart_hour("19:00");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
String [] _eHours = getResources().getStringArray(R.array.Ending_Hours);
ArrayAdapter<String> adp2=new ArrayAdapter<String>
(this, Android.R.layout.simple_spinner_item,_eHours);
adp2.setDropDownViewResource(Android.R.layout.simple_spinner_dropdown_item);
eHour.setAdapter(adp2);
eHour.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Courses a = new Courses();
switch(arg2) {
case 0 :
course.setEnd_hour("9:00");
break;
case 1 :
course.setEnd_hour("9:30");
break;
case 2 :
course.setEnd_hour("10:00");
break;
case 3:
course.setEnd_hour("10:30");
break;
case 4:
course.setEnd_hour("11:00");
break;
case 5:
course.setEnd_hour("11:30");
break;
case 6:
course.setEnd_hour("12:00");
break;
case 7:
course.setEnd_hour("12:30");
break;
case 8:
course.setEnd_hour("13:00");
break;
case 9:
course.setEnd_hour("13:30");
break;
case 10:
course.setEnd_hour("14:00");
break;
case 11:
course.setEnd_hour("14:30");
break;
case 12:
course.setEnd_hour("15:00");
break;
case 13:
course.setEnd_hour("15:30");
break;
case 14:
course.setEnd_hour("16:00");
break;
case 15:
course.setEnd_hour("16:30");
break;
case 16:
course.setEnd_hour("17:00");
break;
case 17:
course.setEnd_hour("17:30");
break;
case 18:
course.setEnd_hour("18:00");
break;
case 19:
course.setEnd_hour("18:30");
break;
case 20:
course.setEnd_hour("19:00");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
add_class.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
course.setCourse_name(cName.getText().toString());
course.setClassroom(cRoom.getText().toString());
course.setTeacname(tName.getText().toString());
/* Toast.makeText(getApplicationContext(),course.getCourse_name(),Toast.LENGTH_LONG ).show();
Toast.makeText(getApplicationContext(), course.getClassroom().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), course.getTeacname().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplication(), course.getDay(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplication(), course.getStart_hour(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplication(), course.getEnd_hour(), Toast.LENGTH_LONG).show();*/
/*data.putExtra("crs",new Courses(cName.getText().toString(),cRoom.getText().toString(),tName.getText().toString(),
gun.getSelectedItem().toString(),sHour.getSelectedItem().toString(),eHour.getSelectedItem().toString()));*/
Intent data = new Intent(Courses.this,MainActivity.class);
data.putExtra("ders",course);
setResult(RESULT_OK,data);
finish();
}
});
}
}
これがMainActivity.Javaです
package com.example.classprogram;
import Java.util.ArrayList;
import Java.util.List;
import Android.os.Bundle;
import Android.app.Activity;
import Android.content.Intent;
import Android.view.Menu;
import Android.view.View;
import Android.view.ViewGroup.LayoutParams;
import Android.widget.AdapterView;
import Android.widget.ArrayAdapter;
import Android.widget.Button;
import Android.widget.EditText;
import Android.widget.GridLayout;
import Android.widget.Spinner;
import Android.widget.Toast;
import Android.widget.AdapterView.OnItemSelectedListener;
import Android.widget.TextView;
public class MainActivity extends Activity {
public GridLayout glayout;
protected String _COURSENAME ;
protected String _CLASSROOM ;
protected String _TCHRNAME ;
protected String _DAY ;
protected String _SHOUR ;
protected String _EHOUR ;
//private Bundle extras[] ;
int THIS = 1;
TextView[][] indexes = new TextView[7][24];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
glayout = (GridLayout)findViewById(R.id.grid);
glayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent (getApplicationContext(),Courses.class);
startActivityForResult(in,THIS);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==THIS){
if(resultCode==RESULT_OK){
// Bundle d = getIntent().getExtras();
//Courses _course = d.getParcelable("crs");
Intent x = getIntent();
Courses _course = (Courses) x.getParcelableExtra("course");
Toast.makeText(getApplicationContext(), _course.getCourse_name(), Toast.LENGTH_LONG).show();
/*for(int i=0;i<glayout.getColumnCount();i++){
for(int j=0; i<glayout.getRowCount();i++){
indexes[i][j]=indexes[i][j];
if(glayout.getChildAt(i).getTag()==_course.getDay() && glayout.getChildAt(j).getTag()==_course.getStart_hour()){
glayout = (GridLayout)findViewById(R.id.grid);
indexes[i][j].setBackgroundColor(993300);
}
}
}*/
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
およびLogcatレポート
FATAL EXCEPTION: main
at Android.app.ActivityThread.deliverResults(ActivityThread.Java:3367)
at Android.app.ActivityThread.handleSendResult(ActivityThread.Java:3410)
at Android.app.ActivityThread.access$1100(ActivityThread.Java:141)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1304)
at Android.os.Handler.dispatchMessage(Handler.Java:99)
at Android.os.Looper.loop(Looper.Java:137)
at Android.app.ActivityThread.main(ActivityThread.Java:5103)
at Java.lang.reflect.Method.invokeNative(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:525)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: Java.lang.NullPointerException
at com.example.classprogram.MainActivity.onActivityResult(MainActivity.Java:69)
at Android.app.Activity.dispatchActivityResult(Activity.Java:5322)
at Android.app.ActivityThread.deliverResults(ActivityThread.Java:3363)
12-29 10:58:32.392: E/AndroidRuntime(1947): ... 11 more
この3つのコードに問題があると思いますが、解決できません。よろしくお願いします。
Intent x = getIntent();
Courses _course = (Courses) x.getParcelableExtra("course");
Toast.makeText(getApplicationContext(), _course.getCourse_name(),
Toast.LENGTH_LONG).show();
onActivityResult
メソッドには、Intent
のパラメーターがあります。
onActivityResult
でこれを行います:-
Bundle bundle = data.getExtras();
このバンドルオブジェクトを使用してエクストラを取得できます
OnActivityResult()で最初にスーパーを呼び出します。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//first call super
super.onActivityResult(requestCode, resultCode, data);
//rest of your code
if(requestCode==THIS) {
そして確かに代わりに
Intent x = getIntent();
行う:
Intent x = data;
Courses.Javaで、MainActivityを開始するための3行目を追加しました。
Intent data = new Intent(Courses.this,MainActivity.class);
data.putExtra("ders",course);
startActivity(data);
MainActivity.Javaで、「course」を「ders」に置き換えました。bcozキー値は「ders」です。
Intent x = getIntent();
Courses _course = (Courses) x.getParcelableExtra("ders");
行を置き換えます
Intent x = getIntent();
to Intent x = data;
onActivityResultに条件データ!= nullを追加すると、次のようになります。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != RESULT_CANCELED && data != null) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
-----
}
}
}