I have two classes these names are Courses and MainActivity .MainActivity has LAUNCHER activity and it has Gridlayout. Courses has DEFAULT activity.When GridLayout clicked Courses Interface is openning and user is filling the views according to his or her course program so Courses object is created.I would like to send this object to MainActivity class.When I sending this object I'm getting NullPointerException.I tried to solve this problem but I can not.I need a proffesional help.
here is the 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();
}
});
}
}
here is the 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;
}
}
and Logcat report
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
I think problem is in the these three code but I can not solve.Please help me thanks you in advance for helping.
Intent x = getIntent();
Courses _course = (Courses) x.getParcelableExtra("course");
Toast.makeText(getApplicationContext(), _course.getCourse_name(),
Toast.LENGTH_LONG).show();
in onActivityResult
method, there is a paramater for Intent
.
Do this in onActivityResult
:-
Bundle bundle = data.getExtras();
they you can retrieve the extra's using this bundle object
Call super first thing in 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) {
And indeed instead of
Intent x = getIntent();
do:
Intent x = data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With