UPDATE: So I forgot to clarify this, but the button is at first non-existent on the App UNTIL dynamically added. Once the layout has been inflated with the button, the button then causes the crash. I apologize for the issues that came up from this development.
So there is a button that keeps crashing my program no matter what (unless I remove the onClick). I tested every line in my Java code to see what was causing it but even after leaving it empty, it still crashes it.
From my MainActivity.Java, the method that causes the crash:
public void changeState(View v) {
String owe = "O";
String debt = "D";
button = (Button) findViewById(R.id.decl);
if (button.getText().toString().equals("D")) {
button.setText("O");
} else {
button.setText(("D"));
}
}
Also from the Java file, the layout inflation method that creates the button causing the crashes:
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}
The above Java code inflates this xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#CCFFFF"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="81dp"
android:orientation="horizontal">
<EditText
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:layout_weight="2"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:padding="14dp"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F" />
<Button
android:id="#+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
<!--android:onClick="changeState"-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">\
<EditText
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="(Ex 123.25)"
android:inputType="numberDecimal"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
<EditText
android:id="#+id/owed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="(Ex $, coffee)"
android:shadowColor="#0066FF"
android:textColor="#000000"
android:textColorHint="#8F8F8F"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:orientation="vertical"
android:background="#CFCFCF"
/>
The Button:
<Button
android:id="#+id/decl"
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="D"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:onClick="changeState"
/>
Crash log
08-09 22:44:22.792 1951-1951/com.xephos.detra E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xephos.detra, PID: 1951
java.lang.IllegalStateException: Could not find a method changeState(View) in the activity class android.app.Application for onClick handler on view class android.widget.Button with id 'decl'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at ...
Edit:
Put this in your MainActivity after setContentView():
public void create(MenuItem v) {
newContact = new LinearLayout(this);
newContact.setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.newcontact, null);
Button b = (Button)view.findViewById(R.id.decl);
if (b != null) {
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
if ("D".equals(b.getText().toString())) {
b.setText("O");
} else {
b.setText(("D"));
}
}
});
}
view.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeRight() {
}
});
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert);
insertPoint.addView(view);
}
Related
I have a nav fragment section called GroupbookFragment which uses a TabLayout with 3 Tabs.
(GroupbookKrippeFragment, GroupbookHafenFragment & GroupbookKindergartenFragment)
Each Tab has a CardView with a RecyclerView in it. The goal is to have a global "Add Child" Button on the GroupbookFragment level, which forward you to another activity with a form where you can create a new child. After filling data & clicking a save button, you'll come back to the GroupbookFragmentview and the new created data from the form will show up in the correct CardView within one of the three corresponding fragments.
So far I successfully implemented all functionalities when the methods and "Add child" button are already IN the corresponding tab child fragment (e.g. GroupbookKrippeFragment). The problem here is, whenever I change the tab from the tabLayout, the "Add Child" button is moving away aswell. So after moving all methods & the button into the parent fragment GroupbookFragment I can navigate through all pages and fill the form & save, but the recyclerView won't update and show the new created data in the child's recyclerView anymore?!
Thanks in advance for any help! Here is the code view:
GroupbookFragment.java
public class GroupbookFragment extends Fragment {
// initializing elements
RecyclerView recyclerView;
ChildListAdapter childListAdapter;
List<Child> children = new ArrayList<>();
RoomDb database;
FloatingActionButton addChildBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_groupbook, container, false);
// inflate the layout for GB Krippe fragment
View viewKrippe = inflater.inflate(R.layout.fragment_groupbook_krippe, container, false);
// link recycler view to correct element
recyclerView = viewKrippe.findViewById(R.id.recycler_view_krippe);
// Tab layout initialization
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
GroupbookAdapter adapterGroupBook = new GroupbookAdapter(getChildFragmentManager(), getLifecycle());
viewPager.setAdapter(adapterGroupBook);
// set 3 Tab titles
tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add Child code
database = RoomDb.getInstance(getContext());
children = database.mainDAO().getAll();
updateRecycler(children);
// link var to app element & set on click route
addChildBtn = view.findViewById(R.id.child_add_btn);
addChildBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupbookFragment.this.getContext(), AddChildActivity.class);
startActivityForResult(intent, 101);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Child new_child = (Child) data.getSerializableExtra("child");
database.mainDAO().insert(new_child);
children.clear();
children.addAll(database.mainDAO().getAll());
childListAdapter.notifyDataSetChanged();
}
}
}
void updateRecycler(List<Child> children) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(GroupbookFragment.this.getContext()));
childListAdapter = new ChildListAdapter(GroupbookFragment.this.getContext(), children, childClickListener);
recyclerView.setAdapter(childListAdapter);
}
private final ChildClickListener childClickListener = new ChildClickListener() {
#Override
public void onClick(Child child) {
}
#Override
public void onLongClick(Child child, CardView cardView) {
}
};
}
GroupbookFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorAnimationMode="elastic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tabLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager_groupBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- Add floating button to add child-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/child_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="#dimen/margin_cardView"
android:backgroundTint="#color/green"
android:src="#drawable/ic_add_child"
app:tint="#color/white" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
GroupbookKrippeFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Groupbook.GroupbookKrippeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/groupBook_krippe_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/margin_cardView"
app:cardCornerRadius="#dimen/cornerRadius_Card"
app:cardElevation="#dimen/elevation_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/groupBook_krippe_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/creme_500"
android:layout_weight="6"
android:padding="#dimen/padding_default">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline_group_1_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<ImageView
android:id="#+id/ic_groupBook_krippe_1"
android:layout_width="#dimen/iconSize_groupBook_header"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_groupbook_krippe_1_logo"
app:tint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline_group_1_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/title_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gruppe 1"
android:textAllCaps="true"
android:textSize="#dimen/textSize_groupBook_title"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/desc_groupBook_krippe_1"/>
<TextView
android:id="#+id/desc_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gesamt: 12 | Anwesend: 10"
android:textSize="#dimen/textSize_groupBook_subTitle"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/title_groupBook_krippe_1"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="#dimen/padding_default"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="ID"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Vorname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nachname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtstag"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtsort"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Konfession"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nationalität"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
</TableRow>
</TableLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_krippe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
I'm trying to create a form to capture data, I need to access the camera to take pictures. My problem is when I try to call the camera with imagenButton, it's not working but the code doesn't show any error.
Here's my fragment to declare form:
public class EspecimenesInsertarFragment extends Fragment implements
Response.Listener<JSONObject>, Response.ErrorListener {
private EspecimenesViewModel especimenesViewModel;
RequestQueue rq;
JsonRequest jrq;
final int COD_SELECCIONA=10;
final int COD_FOTO=20;
private static final int REQUEST_IMAGE_CAMERA=101;
private static final int REQUEST_PERMISSION_CAMERA=101;
Button btnGuardar, btnCancelar;
ImageButton btnCamara, btnGaleria;
ImageView imageView;
public static final int MY_DEFAULT_TIMEOUT = 50000;
public EspecimenesInsertarFragment() {
// Required empty public constructor
}
public static EspecimenesInsertarFragment newInstance() { return new EspecimenesInsertarFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.fragment_colecciones_insertar, container, false);
imageView = (ImageView)view.findViewById(R.id.imageView);
btnCamara=(ImageButton) view.findViewById(R.id.tomarFoto);
btnGuardar = (Button) view.findViewById(R.id.buttonIngresarEspecimenes);
rq = Volley.newRequestQueue(getContext());
/* if(validaPermisos()){
btnCamara.setEnabled(true);
}else{
btnCamara.setEnabled(false);
} */
if (ContextCompat.checkSelfPermission(getContext(), WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA}, 1000);
}
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registrar_especimen("http://localhost/BIO-UES-APP/EspecimenesController.php");
}
});
static final int REQUEST_TAKE_PHOTO=1;
public void tomarFoto(View view){
Intent takePictureInent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Log.d("entra","");
if(takePictureInent.resolveActivity(getActivity().getPackageManager())!= null){
File photoFile=null;
try {
photoFile=createImageFile();
}catch (IOException ex){
}
if(photoFile!=null){
Uri photoUri=FileProvider.getUriForFile(getActivity(),"com.example.luvin.drawercero",photoFile);
takePictureInent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
getActivity().startActivityForResult(takePictureInent,REQUEST_TAKE_PHOTO);
}
}
}
And this is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Especimenes.EspecimenesConsultarFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="-7dp"
android:layout_marginRight="-7dp"
android:layout_marginBottom="9dp"
tools:layout_editor_absoluteX="393dp"
tools:layout_editor_absoluteY="79dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="#+id/tomarFoto"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="100dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:clickable="true"
android:src="#android:drawable/ic_menu_camera" />
<ImageButton
android:id="#+id/seleccionarDesdeGaleria"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="200dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:src="#android:drawable/ic_menu_gallery" />
</FrameLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="88dp"
android:orientation="vertical">
<Button
android:id="#+id/buttonCancelarEspecimenes"
android:layout_width="129dp"
android:layout_height="41dp"
android:layout_toRightOf="#+id/buttonIngresar"
android:backgroundTint="#96A6A8"
android:gravity="center|left"
android:text="CANCELAR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.783"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.659" />
<Button
android:id="#+id/buttonIngresarEspecimenes"
android:layout_width="129dp"
android:layout_height="41dp"
android:layout_marginStart="68dp"
android:text="INGRESAR"
app:backgroundTint="#00BCD4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/buttonCancelar"
app:layout_constraintHorizontal_bias="0.85"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.659" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
Logcat message when button is pressed:
com.example.luvin.drawercero D/ViewRootImpl#6731d38[MainActivity]: ViewPostIme pointer 1
To complete my question, I have the permissions in the manifest. I already tried to call the camera in another way, tried creating the method in the MainActivity to create the OnClick event in the XML Layout, but nothing works.
If anyone knows how to solve this issue please help.
Thanks.
You forgot to add the on click listener to your photo button. This should solve your problem:
btnCamara = view.findViewById(R.id.tomarFoto);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tomarFoto(v);
}
});
I'm pretty new to Android Studio and I'm having difficulty starting a new activity. I've triple checked my code and I can't figure out what my problem is. I've also google searched for a couple of hours but nobody seems to have had the same problem as me. Logcat isn't reporting a problem, but when I run the app, and click the button, the app goes to a blank screen. Please help!
public class OriginalFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_original, container, false);
return rootView;
}
}
public class MainFragment extends Fragment {
private AlertDialog mDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Sets up the about button
View aboutButton = rootView.findViewById(R.id.about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.about_title);
builder.setMessage(R.string.about_text);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
mDialog=builder.show();
}
});
View originalButton = rootView.findViewById(R.id.original_button);
originalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), OriginalActivity.class);
getActivity().startActivity(intent);
}
});
View pictureButton = rootView.findViewById(R.id.picture_button);
originalButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(getActivity(), PictureActivity.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
#Override
public void onPause(){
super.onPause();
if (mDialog != null)
mDialog.dismiss();
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<FrameLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:clipChildren= "false"
tools:context= "org.example.abstract_art.MainActivity">
<fragment android:id= "#+id/main_fragment"
class= "org.example.abstract_art.MainFragment"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
tools:layout= "#layout/fragment_main" />
</FrameLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/select_shapes_background"
tools:context="org.example.abstract_art.OriginalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Shapes"
android:id="#+id/textView"
android:textSize="#dimen/menu_text_size"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="org.example.abstract_art.OriginalFragment"
android:id="#+id/fragment"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
tools:layout="#layout/fragment_original" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu_background"
android:elevation="#dimen/elevation_high"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context=".org.example.abstract_art.MainFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/menu_space"
android:text="#string/long_app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="#dimen/menu_text_size"/>
<Button
android:id="#+id/original_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/original_label"/>
<Button
android:id="#+id/picture_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/picture_label"/>
<Button
android:id="#+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/about_label"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu_background"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context="org.example.abstract_art.OriginalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circles"
android:id="#+id/circlesText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rectangles"
android:id="#+id/rectanglesText"
android:layout_below="#+id/circlesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squares"
android:id="#+id/squaresText"
android:layout_below="#+id/rectanglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triangles"
android:id="#+id/trianglesText"
android:layout_below="#+id/squaresText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4 Point Stars"
android:id="#+id/fourStarText"
android:layout_below="#+id/trianglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 Point Stars"
android:id="#+id/sixStarText"
android:layout_below="#+id/fourStarText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Art!"
android:id="#+id/createOriginalButton"
android:layout_below="#+id/sixStarText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please Make sure Below Things
You have Define Main Activity, OriginalActivity,PictureActivity in your Manifest file
Please Make sure You also set Layout in all Activity or Fragment and Bind Components Of All Layout in that class
try below code
Button originalBtn; //Define class Level
originalBtn=(Button)rootView.findViewById(R.id.original_button);
Now you can set click Listener and code in it
Second main things that you define two times originalButton Click Listener so i think thats why you get problem
I think second Listener should be pictureButton Click Listener
I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);
As the title says, I need to get the value of an editText contained in a fragment activity, being part of a ViewPager; I've tried everything so far, and I've understood that the xml items can be accessed just by passing the current view of the activity after inflating it, and that's what i've done.. But i need to have the values contained in the edittexts after the user fills it, specifically after a button 'save' is pressed; during the execution of the program i get this error when i try to get the text contained: "java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference"
Sorry for the silly question, i'm new to android programming and i appreciate all the help i can get
Here's the code of the fragment activity:
public class Monday extends android.support.v4.app.Fragment{
EditText[] subjects = new EditText[6];
EditText[] classes = new EditText[6];
private OnFragmentInteractionListener mListener;
public Monday() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ScrollView scrollView = (ScrollView)inflater.inflate(R.layout.fragment_monday, container, false);
//some code
subjects[0] = (EditText) scrollView.findViewById(R.id.editTextMateria1);
subjects[1] = (EditText) scrollView.findViewById(R.id.editTextMateria2);
subjects[2] = (EditText) scrollView.findViewById(R.id.editTextMateria3);
subjects[3] = (EditText) scrollView.findViewById(R.id.editTextMateria4);
subjects[4] = (EditText) scrollView.findViewById(R.id.editTextMateria5);
subjects[5] = (EditText) scrollView.findViewById(R.id.editTextMateria6);
classes[0] = (EditText) scrollView.findViewById(R.id.editTextClasse1);
classes[1] = (EditText) scrollView.findViewById(R.id.editTextClasse2);
classes[2] = (EditText) scrollView.findViewById(R.id.editTextClasse3);
classes[3] = (EditText) scrollView.findViewById(R.id.editTextClasse4);
classes[4] = (EditText) scrollView.findViewById(R.id.editTextClasse5);
classes[5] = (EditText) scrollView.findViewById(R.id.editTextClasse6);
return scrollView;
//return inflater.inflate(R.layout.fragment_monday, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
public String[] calculateDay(){
String s = "";
for(int i=1;i<subjects.length;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
}
String[] monday = new String[6];
monday = s.split(";");
return monday;
}
}
That's the part that generates the error:
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
Here's the code of the 'mainactivity' that generates the ViewPager(on which the button pressed save is handled):
public class CustomTimetables extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{
public static String classname = "";
ViewPager viewPager = null;
private MyAdapter2 mAdapter;
ViewPager Tab;
MyAdapter2 TabAdapter;
ActionBar actionBar;
PagerSlidingTabStrip tabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_custom_timetables);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
Tab = (ViewPager) findViewById(R.id.pager);
TabAdapter = new MyAdapter2(getSupportFragmentManager());
Tab.setAdapter(TabAdapter);
tabs.setViewPager(Tab);
Toast.makeText( getApplicationContext(),
"Inserisci l'orario nelle caselle, lasciale vuote se le lezioni terminano prima ",
Toast.LENGTH_LONG
).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_custom_timetables, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onSaveTimetable(View v){
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(this.getSupportFragmentManager(), "insert_class");
}
public void onDialogPositiveClick(DialogFragment dialog) {
String timetable = "Orario;Lunedi;Martedi;Mercoledi;Giovedi;Venerdi;Sabato;\n08:00;";
String[] monday = new String[6];
Monday m = new Monday();
monday = m.calculateDay();
for(int i = 0; i<6; i++){
timetable += monday[i]+";";
switch(i){
case 0: timetable +="\n";break;
case 1 : timetable +="\n09:00;";break;
case 2 : timetable +="\n10:00;";break;
case 3 : timetable +="\n11:05;";break;
case 4 : timetable +="\n12:00;";break;
case 5 : timetable +="\n12:50;";break;
}
}
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
}
class MyAdapter2 extends FragmentStatePagerAdapter
{
public MyAdapter2(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
if(i==0){
fragment = new Monday();
}
if(i==1){
fragment = new Tuesday();
}
if(i==2){
fragment = new Wednesday();
}
if(i==3){
fragment = new Thursday();
}
if(i==4){
fragment = new Friday();
}
if(i==5){
fragment = new Saturday();
}
return fragment;
}
#Override
public int getCount() {
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return "Lunedi";
}
if(position == 1){
return "Martedi";
}
if(position == 2){
return "Mercoledi";
}
if(position == 3){
return "Giovedi";
}
if(position == 4){
return "Venerdi";
}
if(position == 5){
return "Sabato";
}
return null;
}
}
Here's the logcat:
06-09 19:29:16.172 24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-09 19:29:16.187 24414-24414/com.progettostage.nick__000.timetablesapp D/Atlas﹕ Validating map...
06-09 19:29:16.250 24414-24443/com.progettostage.nick__000.timetablesapp I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
06-09 19:29:16.252 24414-24443/com.progettostage.nick__000.timetablesapp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-09 19:29:16.290 24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Enabling debug mode 0
06-09 19:32:20.725 24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ Application requested CPU execution
06-09 19:32:20.738 24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ 0xb83ba0b8 Launching thread(s), CPUs 4
06-09 19:32:38.415 24414-24414/com.progettostage.nick__000.timetablesapp D/AndroidRuntime﹕ Shutting down VM
06-09 19:32:38.422 24414-24414/com.progettostage.nick__000.timetablesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.progettostage.nick__000.timetablesapp, PID: 24414
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.progettostage.nick__000.timetablesapp.Monday.calculateDay(Monday.java:111)
at com.progettostage.nick__000.timetablesapp.CustomTimetables.onDialogPositiveClick(CustomTimetables.java:111)
at com.progettostage.nick__000.timetablesapp.NoticeDialogFragment$2.onClick(NoticeDialogFragment.java:33)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
And this is simply the class that handles the dialog, i dont think it has anything to do with it, but better safe than sorry..
public class NoticeDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_classinput, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setTitle("Inserisci nome classe")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText)view.findViewById(R.id.classname22);
CustomTimetables.classname = et.getText().toString();
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create();
}
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
NoticeDialogListener mListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
}
Here's the Monday.xml as well(had to cut off some parts in the end cause it was too long, but it's simple, it goes on like this till editTextMateria6 and editTextClasse6):
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Monday"
>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8:00-9:00"
android:id="#+id/PrimaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="115dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTextMateria1"
android:layout_marginTop="160dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="#+id/classe1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="225dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextClasse1"
android:layout_marginTop="270dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
-----------------------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="9:00-10:00"
android:id="#+id/SecondaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="325dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="390dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextMateria2"
android:layout_marginTop="435dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="#+id/classe2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextClasse2"
android:layout_marginTop="545dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
-----------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="10:00-10:50"
android:id="#+id/TerzaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="600dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="665dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextMateria3"
android:layout_marginTop="710dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="#+id/classe3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="775dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextClasse3"
android:layout_marginTop="825dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
-----------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="11:05-12:00"
android:id="#+id/QuartaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="875dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="940dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextMateria4"
android:layout_marginTop="985dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="#+id/classe4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1050dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextClasse4"
android:layout_marginTop="1095dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
------------------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12:00-12:50"
android:id="#+id/QuintaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1150dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1215dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextMateria5"
android:layout_marginTop="1260dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="#+id/classe5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1325dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/editTextClasse5"
android:layout_marginTop="1380dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
----------------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12:50-13:40"
android:id="#+id/SestaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1435dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="#+id/materia6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1500dp" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:width="400dp"
android:height="70dp"
android:onClick="onSaveTimetable"
android:text="Salva"
android:layout_marginTop="1700dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
UPDATE
Okay I've added a few lines so maybe it is more clear what the problem is..
String sub1 = subjects[0].getText().toString();
String classr1 = classes[0].getText().toString();
for(int i=0;i<subjects.length;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
}
If I set it like this, I get the same exact error right at the sub1 line, because in a method like this one i'm not able to pass the view or inflate that, as I did on oncreateview, where it works just fine whatever operation I do.. The problem is that I need those edittext after the user had put something inside them.. I hope i've been a little clearer, I appreciate all the help I'm still getting and I'd like to point out that I've tried every single solution down here, even tho I didn't reply to them all
Array index starts from 0. Please make sure you use correct index values when accessing arrays.
This is wrong usage:
Monday m = new Monday();
monday = m.calculateDay();
Try this:
Monday m = mAdapter.getItem(viewPager.getCurrentItem());
monday = m.calculateDay();
It has been said before, but I'll say it again.
for(int i=1;i<7;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
Should be
for(int i = 0; i < subjects.length; i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
EDIT: To be honest, I'd just write the code like this instead
public enum Days {
MONDAY("Lunedi") { //should be string resource
public Fragment createFragment() {
return new Monday();
}
},
TUESDAY("Martedi") {
public Fragment createFragment() {
return new Tuesday();
}
},
WEDNESDAY("Mercoledi") {
public Fragment createFragment() {
return new Wednesday();
}
},
THURSDAY("Giovedi") {
public Fragment createFragment() {
return new Thursday();
}
},
FRIDAY("Venerdi") {
public Fragment createFragment() {
return new Friday();
}
},
SATURDAY("Sabato") {
public Fragment createFragment() {
return new Saturday();
}
};
private String name;
private Days(String name) {
this.name = name;
}
public String getName() { return name; }
public abstract Fragment createFragment();
}
class MyAdapter2 extends FragmentStatePagerAdapter
{
public MyAdapter2(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
Days[] days = Days.values();
if(i < days.length) {
fragment = days[i].createFragment();
}
return fragment;
}
#Override
public int getCount() {
return Days.values().length;
}
#Override
public CharSequence getPageTitle(int position) {
Days[] days = Days.values();
if(position < days.length) {
return days[position].getName();
} else {
return null;
}
}
}
And to be fair, and most importantly, subjects[5] = (EditText) scrollView.findViewById(R.id.editTextMateria6); is null because editTextMateria6 does not exist in fragment_monday.xml, and neither does editTextClasse6.
You can maybe try using the :
getSupportFragmentManager().findFragmentById
to get hold of the already initiated Monday fragment in your onDialogPositiveClick. You can then use this fragment to invoke the calculateDay method. An id can be set in the xml file for the fragment. Your current implementation to create an instance and invoke calculateDay is incorrect.
In the xml layout of your Activity where I am assuming you've added the fragment tag, add an id attribute. Then in your onDialogPositiveClick, call
Monday monday = (Monday) getSupportFragmentManager().
findFragmentById("that fragment id here");
Now, use this monday variable to invoke the calculateDay method.
Some official documentation: http://developer.android.com/training/basics/fragments/communicating.html#Deliver