findVIewById(R.id.My_id) returns null. setContentView(R.layout.my_custom_layout) works, but how to return? - java

Making small data organizer.
Upon clicking on item appears custom dialog, for which i've created custom xml.
The xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.8"
android:background="#drawable/background"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="12dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Edit Item"
android:textAlignment="center"
android:textSize="20sp" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|center_horizontal|center_vertical">
<Space
android:layout_width="wrap_content"
android:layout_height="6dp"
android:layout_weight="1" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/item_edit_entity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Entity"
android:maxLength="30"
android:visibility="visible" />
<Space
android:layout_width="wrap_content"
android:layout_height="6dp"
android:layout_weight="1" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/Item_edit_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Value"
android:maxLength="30" />
<Space
android:layout_width="wrap_content"
android:layout_height="12dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Space
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</com.google.android.material.textfield.TextInputLayout>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
So, its custom xml, it works. I started to add functionality, and face a problem.
private final static String FILE_NAME = "content.txt";
public void okClicked() {
Toast.makeText(getApplicationContext(), "Item Saved!",
Toast.LENGTH_LONG).show();
FileOutputStream fos = null;
try {
#SuppressLint("WrongViewCast") EditText textBox = findViewById(R.id.item_edit_entity);
#SuppressLint("WrongViewCast") EditText textBox2 = findViewById(R.id.Item_edit_value);
String text = textBox.getText().toString();
String text2 = textBox2.getText().toString();
String text3=text2+"||"+text;
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(text3.getBytes());
Toast.makeText(this, "File Saved", Toast.LENGTH_SHORT).show();
}
catch(IOException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
finally{
try{
if(fos!=null)
fos.close();
}
catch(IOException ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
This code supposed to take text from EditText positions, each of them have a id.
But findViewById returns null. In general that is the main issue need to be solved. :)
Oh, i've read a lot of stackoverflow articles, and searched the internet.
I've tried to use setContentView(R.layout.my_custom_xml) and it worked.
But then another problem appears - after things done the screen becomes as in layout my_custom_xml.
Trying to switch back on setContentView(R.layout.activity_main) leads to empty screen beacuse all the content is added via code and not in the xml file.
So, questions: how to access R.id.my_Custom_id_in_custom_xml from code? Possible without switching by setContentView. If is the main an the right solution, then how to restore window stance as it was before clicking. Or how to rebuild (call?) the main screen if other options is impossible. Can i just call the onCreate?

I guess you might be calling the wrong View
just replace <com.google.android.material.textfield.TextInputEditText
with <EditText.
Or in you class file replace EditText with TextInputEditText, and it will solve your issue.
the error is occurring because you are implementing the TextInputEditText but calling it as EditText.
Happy coding ✌️

Related

Android multi window launcher sidebar

I want this drawerLayout(bellow photo) to always open like Samsung multiwindow sidebar.
Till now I am able to open this drawerLayout(sidebar) within the app. please give the instruction to be able to open this draweLayout(sidebar) from anywhere(I mean from any other app or even from home screen of a device).
Thanks in advance.
Here is my drayerLayout XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#color/cardview_light_background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swipe Right"
android:textSize="15dp"
android:textColor="#D50000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open App In\nMultiWIndow Mode"
android:textSize="30sp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#color/black"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity2">
<ListView
android:id="#+id/listView"
android:layout_width="280dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
This cannot be achieved by making something global you to do some modification in every activity
you can create a new class that will have a method to initialize the navigation, move navigation view to separate xml file and include that file in every activity, and call initialization method in every activity
You can try this library https://github.com/mikepenz/MaterialDrawer to move most xml code to java so you won't have to add DrawerLayout in every activity but choice is your, it always better to skip using 3rd party libraries if you can
if you are using library than here's some example you can follow
Constant.java
private void setupNavigationDrawer() {
ProfileDrawerItem section = new ProfileDrawerItem()
.withSetSelected(false)
.withSetSelected(false)
.withEnabled(false);
PrimaryDrawerItem joyType0DrawerItem = new PrimaryDrawerItem().withName("Basic Gamepad").withTag(KEY_BASIC_GAMEPAD_1).withLevel(2);
PrimaryDrawerItem joyType1DrawerItem = new PrimaryDrawerItem().withName("Assault Horizon").withTag(KEY_ASSAULT_HORIZON).withLevel(2);
PrimaryDrawerItem joyType2DrawerItem = new PrimaryDrawerItem().withName("Awesomenauts").withTag(KEY_AWESOMENAUTS).withLevel(2);
PrimaryDrawerItem joyType3DrawerItem = new PrimaryDrawerItem().withName("BombSquad").withTag(KEY_BOMBSQUAD).withLevel(2);
PrimaryDrawerItem joyType4DrawerItem = new PrimaryDrawerItem().withName("Final Fantasy XIII").withTag(KEY_FINAL_FANTASY_XIII).withLevel(2);
SectionDrawerItem sectionDrawerItem = new SectionDrawerItem().withName("Joystick Type").withTextColorRes(R.color.colorAccent);
PrimaryDrawerItem deviceDrawerItem = new PrimaryDrawerItem().withName("Device Connection").withTag(KEY_DEVICE_CONNECTION).withIcon(GoogleMaterial.Icon.gmd_devices);
PrimaryDrawerItem settingsDrawerItem = new PrimaryDrawerItem().withName("Settings").withTag("settings").withIcon(GoogleMaterial.Icon.gmd_settings);
ndMenu = new DrawerBuilder()
.withActivity(this)
.withToolbar(tbMain)
.withCloseOnClick(true)
.withFireOnInitialOnClick(true)
.withSelectedItemByPosition(6)
.withDelayDrawerClickEvent(300)
.withDisplayBelowStatusBar(true)
.withTranslucentStatusBar(false)
.withOnDrawerItemClickListener(this)
.addDrawerItems(section, deviceDrawerItem, settingsDrawerItem, sectionDrawerItem, joyType0DrawerItem, joyType1DrawerItem, joyType2DrawerItem, joyType3DrawerItem, joyType4DrawerItem)
.build();
findViewById(R.id.nav_button).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
// Check these functions from library example to check drawer state and open close drawer
if(ndMenu.isDrawerOpen){
ndMenu.closeDrawer();
} else {
ndMenu.openDrawer();
}
});
}
MainActivity.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<include
layout="#layout/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout>
nav_view.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
orientation="horizontal">
<Image
id="#+id/nav_button"
android:layout_width="30dp"
android:layout_height="30dp"/>
<LinearLayout>

Adding a Button below a ListView in a ListActivity

I need a ListActivity with a ListView that has a Button below it. So you can scroll the ListView while the Button stays on the bottom. It looks fine in the layout but when i launch the app, there is no Button.
That is the XML of my ListActivity:
<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:descendantFocusability="blocksDescendants"
tools:context=".PalettenActivity" >
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/buttonPalettenBetaetigen"
android:entries="#layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<Button
android:id="#+id/buttonPalettenBetaetigen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Bestätigen" />
</RelativeLayout>
Here's the code of the paletten_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutPalettenItem"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
tools:context=".ChecklistAdapter" >
<TextView
android:id="#+id/textViewPalettenBez"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
android:text="Bezeichnung"
android:textSize="25sp" />
<Button
android:id="#+id/buttonPalettenPlus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="+"
android:textSize="20sp" />
<TextView
android:id="#+id/textViewPalettenAnzahl"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/buttonPalettenPlus"
android:layout_alignBottom="#+id/buttonPalettenPlus"
android:layout_toLeftOf="#+id/buttonPalettenPlus"
android:gravity="center"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/buttonPalettenMinus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/textViewPalettenAnzahl"
android:layout_alignBottom="#+id/textViewPalettenAnzahl"
android:layout_toLeftOf="#+id/textViewPalettenAnzahl"
android:text="-"
android:textSize="20sp" />
</RelativeLayout>
This is how i add the onClickListener to the Button in the onCreate() method of the ListActivity:
LayoutInflater itemInflater (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = itemInflater.inflate(R.layout.activity_paletten, null);
btnPalettenBestaetigen = (Button)convertView.findViewById(R.id.buttonPalettenBetaetigen);
btnPalettenBestaetigen.setText("Bestätigen");
btnPalettenBestaetigen.setVisibility(View.VISIBLE);
btnPalettenBestaetigen.setEnabled(true);
btnPalettenBestaetigen.setClickable(true);
btnPalettenBestaetigen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// do stuff
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
});
Here is my layout with the button visible:
Layout of my ListActivity
Why isn`t the Button visible when i launch the app?
I am glad for any help!
Greetings
Morris F.
SirGrey was right in the end.
That must be a problem with your code... But i can't see the issue, if the activity shows nothing, must be something with with the inflater... – SirGery
The Problem was the Inflater so i did the following changes to my code:
I deleted android:entries="#layout/paletten_item" from the XML of my MainActivity
The MainActivity now extends Activity and no more ListActivity
I setContentView() in my onCreate() method of my MainActivity
I get the ListView by doing listViewPaletten = (ListView)findViewById(R.id.listViewPaletten);
and I dont use the Infalter anymore.
Thanks a lot for your Help!
If you want to scroll your ListView meanwhile the button stays in same position, try to use FloatingActionButton, it must solve your issue
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:entries="#layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fActionButton"
android:layout_width="180dp"
android:layout_height="180dp"
tools:ignore="MissingConstraints,VectorDrawableCompat"/>
Then try this, swap the activity that makes reference, cause this is from my project
<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=".Activities.MainActivities.CartActivity">
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
android:layout_above="#+id/buttonPalettenBetaetigen">
</ListView>
<Button
android:id="#+id/buttonPalettenBetaetigen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Bestätigen" />

Reset results and TextUtil

So, I got a feedback from reviewer and fixed most of my problems but there are two problems that I can’t find solution (I will use bold font to highlight them) the first problem comes after I click result button more than one time because the previous result is added. I know that I should add all the methods checkQuestionOne to checkQuestion6 in the button method but this is not possible so far because the method needs (View or view value) which is not working and I don’t know why so I decided not to include the four Radio buttons methods in it. Second when the checkQuestionFive is blank or there is no value the app crashes I was told to use if (!TextUtils.isEmpty(gettingQuestionFive.getText())), then I add Toast message into the if statement but it still crashes. Any help will be appreciated!!
Thank you!
Java code:
public class MainActivity extends AppCompatActivity {
int health = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkQuestionOne(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_one_yes:
if (checked)
health += 1;
break;
}
}
public void checkQuestionTwo(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_two_yes:
if (checked)
health ++;
break;
}
}
public void checkQuestionThree(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_three_no:
if (checked)
health ++;
break;
}
}
public void checkQuestionFour(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_four_no:
if (checked)
health ++;
break;
}
}
**public void checkQuestionFive**() {
EditText gettingQuestionFive = findViewById(R.id.sleep_hours);
if (!TextUtils.isEmpty(gettingQuestionFive.getText())){
Toast displayError = Toast.makeText(this, "You missed question 5, plase don't leave it blank", Toast.LENGTH_LONG);
displayError.show();
}
int answerQuestionFive = Integer.parseInt(gettingQuestionFive.getText().toString());
if (answerQuestionFive >= 7) {
health ++;
}
}
public void checkQuestionSix() {
CheckBox checkBoxOneA = findViewById(R.id.fruits_check_box);
CheckBox checkBoxOneB = findViewById(R.id.chips_check_box);
CheckBox checkBoxOneC = findViewById(R.id.candy_check_box);
CheckBox checkBoxOneD = findViewById(R.id.all_three_check_box);
if (checkBoxOneA.isChecked() && !checkBoxOneB.isChecked() && !checkBoxOneC.isChecked() && !checkBoxOneD.isChecked()) {
health++;
}
}
**public void overView(View view)**{
checkQuestionFive();
checkQuestionSix();
Toast displayToast = Toast.makeText(this, health + " is your score. If is 4 or above, you have a healthy life", Toast.LENGTH_LONG);
displayToast.show();
health = 0;
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context="com.example.martinevtimov.quizapp2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1. Do you eat healty?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_one_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionOne" />
<RadioButton
android:id="#+id/question_one_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionOne" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group2" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2. Do you work out?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_two_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionTwo" />
<RadioButton
android:id="#+id/question_two_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionTwo" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group3" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3. Do you drink?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_three_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionThree" />
<RadioButton
android:id="#+id/question_three_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionThree" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group4" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4. Do you smoke?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_four_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionFour" />
<RadioButton
android:id="#+id/question_four_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionFour" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5. How many hours do you sleep?"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="#+id/sleep_hours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:inputType="number" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6. What is a healthy snack?"
android:textSize="30sp"
android:textStyle="bold" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fruits"
android:id="#+id/fruits_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chips"
android:id="#+id/chips_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Candy"
android:id="#+id/candy_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All three"
android:id="#+id/all_three_check_box" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="overView"
android:text="Check my health"
android:textAllCaps="true" />
</LinearLayout>
</ScrollView>
You can see the Logcat and the message error I have. The purpose of this app is to make a quiz app with 3 different inputs (EditText, Radio Buttons, and CheckBoxes). When the EditText is left blank, a crash appears. It seems there is some problem with the onClick attribute in your code but I can figure it out. The second problem comes from clicking the check button more than one time than more points are added to the previous points, I tried to add all the question methods into one and then set the global health variable to 0 but is not really working. Also, take a look of the two pictures below my code.
Screen Picture 1
Screen Picture 2

Java.Lang.OutOfMemoryError: Happened only to one layout but not the others

As the title stated, my problem is related to the error.
I have set 3 layout that uses the same background but there's only 1 layout that keeps producing this error. The error started when I set the background. Without the background, it can run with no problem. But the one that I want to solve is how to fix the out of memory error and why does this layout produce the error, while the other two doesn't. The code is pretty much the same.
This is the layout that produces the error.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/seabg">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/f1"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="CLICK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="177dp"
android:id="#+id/btnf"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/ferry"
android:id="#+id/imageView1"
android:layout_marginBottom="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Meanwhile, this is the one that can be run normally without the error. (It should be two layouts, saving some space because the code looks exactly the same only the first ImageView is different.)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/seabg">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/y1"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="CLICK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="177dp"
android:id="#+id/btny"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/yacht"
android:id="#+id/imageView1"
android:layout_marginBottom="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Edit: This is my activity for the one that produce the error.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_f);
final MediaPlayer fSound = MediaPlayer.create(this, R.raw.ferry);
btnf = (Button) findViewById(R.id.btnf);
btnf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fSound.start();
}
});
}
And this is the one that can be run with no problem.
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_y);
final MediaPlayer ySound = MediaPlayer.create(this, R.raw.yacht);
btny = (Button) findViewById(R.id.btny);
btny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ySound.start();
}
});
}
Try removing the imageView or change the image src to a lower resolution! this might solve your issue!

After take a picture and save it, sometimes the program crashes, doesn't return to the activity

I'm calling the function dispatchTakePictureIntent to take a picture. But after I take the picture and save it, the application doesn't returns correctly to the actual activity, the application crashes.
When my button is pressed I call
dispatchTakePictureIntent(1);
then:
public void dispatchTakePictureIntent(int actionCode) {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);*/
}
public File createImageFile() throws IOException {
// Create an image file name
final String nomeForm = "teste";
String imageFileName = nomeForm +".jpeg";
File path = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File image = new File(path,imageFileName );
// String getAbsolutePath = image.getAbsolutePath();
Toast.makeText(this, "Image saved to:\n" + image, Toast.LENGTH_LONG).show();
return image;
}
my logcat error report:
java.lang.RuntimeException: Unable to stop activity {com.example.sform/com.example.sform.mainRisco}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sform/com.example.sform.risco1}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.CompoundButton$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x1. Make sure other views do not use the same id.
Here all my layouts:
Tab Host Layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout_riscos">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
The Tab1:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/risco1"
>
<RelativeLayout
android:id="#+id/geral_risco1"
android:layout_width="match_parent"
android:layout_height="739dp"
android:orientation="vertical"
android:screenOrientation="nosensor" >
<Button
android:id="#+id/pelicula_risco1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="36dp"
android:text="Take a Picture ;)"
android:textSize="30dp" />
<ImageView
android:id="#+id/imageView_risco1"
android:layout_width="9000dp"
android:layout_height="500dp"
android:layout_alignLeft="#+id/pelicula"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true" >
</ImageView>
<RelativeLayout
android:id="#+id/riscoQ1_risco1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</RelativeLayout>
</RelativeLayout>
</ScrollView>
The Tab2:
<TextView android:text="risco 2"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco2"/>
</RelativeLayout>
The Tab3:
<TextView android:text="risco 3"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco3"/>
</RelativeLayout>
The Tab4:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/geral_risco4">
<TextView android:text="risco 4"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco4"/>
</RelativeLayout>
The Tab5:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/geral_risco5" >
<TextView android:text="risco 5"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco5"/>
</RelativeLayout>
Main Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
</RelativeLayout>
Another screen:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:id="#+id/scroll_tela_form"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/info2"
>
<EditText
android:id="#+id/dataAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_below="#+id/textView3"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:layout_toLeftOf="#+id/getDate"
android:ems="10" />
<TextView
android:id="#+id/horaAnaliseFixo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/horaAnalise"
android:layout_below="#+id/dataAnalise"
android:layout_marginTop="8dp"
android:text="Hora de Inicio da Análise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/nomeForm"
android:layout_marginTop="40dp"
android:text="Data da Análise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nomeForm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toLeftOf="#+id/getDate"
android:layout_toRightOf="#+id/textView3"
android:text="NomeForm"
android:textSize="34dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/horaAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/horaAnaliseFixo"
android:layout_toLeftOf="#+id/getDate"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/getDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/horaAnaliseFixo"
android:layout_alignParentRight="true"
android:layout_marginRight="41dp"
android:text="Get Date" />
<Button
android:id="#+id/getTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/getDate"
android:layout_alignParentBottom="true"
android:text="Get Time" />
</RelativeLayout>
</ScrollView>
Another reason for this issue can be two (or more) layout files for the same layout stored under layout, layout-land, layout-w600dp etc. If these layouts have views of different types sharing same id, then you can get this exception two. To reproduce the exception you need to call function dispatchTakePictureIntent(), then rotate your device, then take picture and return back. Does it help?
You have used same id for two different views.
Check your layout XML & use different ids for different views.
It says that you are using the same id for different views and that the id is 0x1. That looks suspiciously like
a) somewhere a view id is expected, something else is given or
b) the R.java is generated incorrectly. Please check if theres an id of 1 in there somewhere.
Did you check the value of
#android:id/tabs
#android:id/tabcontent
?

Categories