Essentially I have an issue with my calendarView widget. In the app whenever it goes from my Main class to the EditProgress class it either freezes or crashes. I tested it, and whenever the calendarView widget is in the EditProgress class the app runs very slowly, and whenever I attempt to use the widget in my .java class it simply crashes. It had complained before saying that it required API level 11, so I tried to fix this by using suppress warnings, but it still crashes or runs slowly. Thank you for any help. Below is my .java class:
package com.dwolford.workoutroutine;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class EditProgress extends Activity {
Button submit;
Button back;
Spinner exercise;
ArrayAdapter<CharSequence> adapter;
EditText reps;
CalendarView calendar;
Context context = this;
#TargetApi(11)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_progress);
exercise = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.Exercises, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
exercise.setAdapter(adapter);
exercise.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText((getBaseContext()), (parent.getItemIdAtPosition(position) + 2) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
calendar = (CalendarView)findViewById(R.id.calendarView);
reps = (EditText)findViewById(R.id.editText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
double repetitions = Double.parseDouble(reps.getText().toString());//Get number of repetitions on that day
String exerciseName = exercise.getSelectedItem().toString();//Get specific exercise user did
String calendarDate = calendar.getDate()+"";
}
});
back = (Button)findViewById(R.id.Back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#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_edit_progress, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is my .XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT PROGRESS"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="45dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:height="50dp"
android:textSize="22dp"
android:gravity="center"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BACK"
android:id="#+id/Back"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:height="50dp"
android:textSize="22dp"
android:background="#drawable/button_gradient" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/Submit"
android:layout_above="#+id/Back"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="38dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:height="50dp"
android:textSize="22dp"
android:background="#drawable/button_gradient" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise"
android:id="#+id/Exercise"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:id="#+id/date"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Repetitions"
android:id="#+id/repetitions"
android:layout_alignBottom="#+id/date"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/repetitions"
android:layout_alignRight="#+id/repetitions"
android:layout_alignEnd="#+id/repetitions"
android:layout_alignLeft="#+id/repetitions"
android:layout_alignStart="#+id/repetitions" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/Exercise"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/date"
android:layout_toLeftOf="#+id/date"
android:layout_alignRight="#+id/Exercise"
android:layout_alignEnd="#+id/Exercise" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_below="#+id/date"
android:layout_centerHorizontal="true" />
Your minimum SDK used for the project is lower than API 11. That's why it's giving you that error.
You can change it in your project settings. Just increase it to API 11 and everything should work fine.
Related
I'm trying to make this small app as a beginner, and in the first page I want to press the login button to go to the login page. I did some search and I used intent and my code has no errors. However, whenever I press the login button, the app crashes, and I don't know why. I tried my best to find out, and this is my last hope here.
Main Class:
package com.example.ali.test;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.b3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),Loginpage.class);
startActivity(intent);
}
});
}
#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_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Login Class:
package com.example.ali.test;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Loginpage extends ActionBarActivity {
Button b1,b2 , b3;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.b3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#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_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
These are the xml files:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/b3"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:layout_alignParentEnd="true" />
</RelativeLayout>
and xml for login
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ali.test.Loginpage">
<TextView android:text="Login" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Enter Name"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_marginTop="46dp"
android:layout_below="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textview"
android:textSize="25dp"
android:layout_toRightOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textview"
android:layout_toStartOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textview"
android:layout_toEndOf="#+id/textview" />
</RelativeLayout>
Thank you.
You are calling setContentView() in your login activity and giving it the same layout file as your main activity. So you are probably getting a crash trying to set something in a view after calling findViewById() and it returned null because no view with that id exists in the main layout.
Try this :
setContentView(R.layout.activity_login);
in your Loginpage activty unstead of calling the same layout that you are using in your MainActivity
*activity_login : refer to the name of the layout of your Loginpage Activity
You simply need to write the correct layout name in your Loginpage.java in setContentView() as you wrote the same layout name of MainActivity.java, so probably when the app tried to find the view id of the views you declared, it didn't found them, and they were considered as null.
I think your app is crashing when you push the button because you have no b3 in xml for login. Change this line in Loginpage to a button that exist:
Button button = (Button) findViewById(R.id.b3);
The crash occurs when loading the Loginpage class. To quickly see if that's the case rename R.id.b3 in the line above to R.id.button and test again.
You might also change this line in MainActivity (change back if it crashes :) ):
Intent intent = new Intent(v.getContext(),Loginpage.class);
to:
Intent intent = new Intent(MainActivity.this,Loginpage.class);
Larry Schiefer/minos23 answer is also needed in order not to crash (but you did this already).
I'm trying to implement a ListView with an embedded Button. I've made my list item view separately and followed this tutorial.
When I try running it, I get an NPE on the button's setOnClickListener because it says it's a null reference. Kindly point out where I've gone wrong or if I need to do anything else.
Here's my code:
Main activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Data">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="319dp"
android:layout_height="232dp"
android:id="#+id/imageView"
android:src="#drawable/image" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:id="#+id/editText"
android:autoText="false"
android:inputType="number"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter value"
android:id="#+id/textView"
android:textSize="24dp"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/button"
android:layout_alignBottom="#+id/editText"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:layout_marginLeft="37dp"
android:layout_marginStart="37dp"
android:onClick="bringList"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select reason:"
android:id="#+id/textView2"
android:textSize="24dp"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
List Item:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listitem"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:id="#+id/listbutton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/listitem"
android:textSize="15dp" />
Java code:
package com.example.abhinav.data;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Data extends ActionBarActivity {
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
ListView lv = (ListView)findViewById(R.id.listView);
makeList();
lv.setAdapter(new MyListAdapter(this, R.layout.list, data));
}
private void makeList() {
for(int i = 1; i<6; i++) {
data.add("Theorem"+i);
}
}
public void bringList() {
}
#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_data, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String> {
private int listlayout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
listlayout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vw = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listlayout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pop up dialog with theorem info
Toast.makeText(getContext(), "Button presss", Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
vw = (ViewHolder) convertView.getTag();
vw.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView title;
Button button;
}
}
Change this Lines
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
to
viewHolder.title = (TextView) convertView.findViewById(R.id.listitem);
viewHolder.button = (Button) convertView.findViewById(R.id.listbutton);
NPE is because you reading view incorrectly.
It seems to me that your List Item might only inflate the TextView element, because it's the actuall root element for the XML.
I recommend wrapping both the TextView and the Button in your list.xml file with a LinearLayout so the actual root view will wrap all your views.
Here's an example of what stacking both views vertically would look like with a LinearLayout for a ListView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="New Text"
android:textSize="24dp" />
<Button
android:id="#+id/listbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/listitem"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Info"
android:textSize="15dp" />
</LinearLayout>
I don't know if this is the case, but you are creating two different ViewHolders. Use vw = new ViewHolder();
And maybe you need to use convertView.findViewById(R.id.listbutton);
I am creating an android application that consists of registration form in dialog. Here, I placed a drop down spinner in registration dialog. But,here it was showing type casting error even i did the correct type casting. Please check it and make it solve. I am trying to solve since 2 hours. This is my code:
package com.example.testingsample;
import com.gc.materialdesign.views.ButtonFlat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button checking,spinner_check;
EditText username,password,confirm,seq_answer;
ButtonFlat clear,register;
Spinner spinner;
String[] questions = {
"What is your first name.?",
"What is your first pet name.?",
"What is your school name.?",
"What is your date of birth.?",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button)findViewById(R.id.button1);
checking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
android.app.Dialog registration_dialog = new android.app.Dialog(MainActivity.this);
registration_dialog.setContentView(R.layout.signup);
registration_dialog.setTitle("MedeQuip Registration");
username = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_username);
password = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_password);
confirm = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_confirmpassword);
spinner = (Spinner)registration_dialog.findViewById(R.id.spinner1);
ArrayAdapter adpter = new ArrayAdapter(MainActivity.this ,android.R.layout.simple_dropdown_item_1line,questions);
spinner.setAdapter(adpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Toast.makeText(MainActivity.this, questions[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);
clear = (ButtonFlat)registration_dialog.findViewById(R.id.dialog_button_clear);
register = (ButtonFlat)registration_dialog.findViewById(R.id.Dialog_button_register);
//seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);*/
/*spinner_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "Checking", Toast.LENGTH_LONG).show();
}
});*/
registration_dialog.show();
}
});
}}
This is my dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="#+id/Dialog_edittext_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_username"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Dialog_edittext_confirmpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_password"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/Dialog_edittext_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="User Name" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="345dp"
android:layout_height="60dp"
android:layout_below="#+id/Dialog_edittext_confirmpassword"
android:layout_centerHorizontal="true" />
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/Dialog_button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/dialog_button_clear"
android:text="Register"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/dialog_button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginRight="52dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="#+id/Dialog_button_register"
android:text="clear"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="Enter your answer for security question" />
</RelativeLayout>
Same problem i faced but i solved that problem with following solutions
-> If you are implementing in Eclipse once clean the project then test it.
-> If above also not works once restart your Eclipse.
Hope it will helps you.
I am trying to add a child view to the main view by pressing a button which is inside the child view but the code doesn't work. Any idea what the problem is. I am beginner in Android
This is the main linear layout with the child linear layout in line with + and - buttons added to it. I would like to add another row under this by pressing + and remove it by pressing -.
Here is the main layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/trainer_main"
android:layout_width="768dp"
android:layout_height="1280dp"
android:background="#121617"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
tools:context=".TrainingProgrammer" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="25dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="#string/title_activity_training_programmer" />
<LinearLayout
android:id="#+id/trainer_headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/interval_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="#string/training_programmer_interval" />
<TextView
android:id="#+id/exercise_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingRight="95dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_exercise" />
<TextView
android:id="#+id/second_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_time" />
<TextView
android:id="#+id/weight_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:textColor="#ffffff"
android:text="#string/training_programmer_weight" />
</LinearLayout>
and here is the child layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trainer_progcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/interval_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="14sp"
android:ems="1"
android:text="1" />
<EditText
android:id="#+id/exercise_content"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:singleLine="true"
android:textColor="#ffffff"
android:hint="#string/training_programmer_exercise_edit"
android:ems="10" />
<EditText
android:id="#+id/time_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="#string/training_programmer_time_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<EditText
android:id="#+id/weight_content"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:hint="#string/training_programmer_weight_edit"
android:inputType="number"
android:maxLength="2"
android:textColor="#ffffff" />
<Button
android:id="#+id/button_minus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="#drawable/minus2" />
<Button
android:id="#+id/button_plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:background="#drawable/plus2" />
and here is the code:
package ca.cruxtec.cruxtectrainer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class ProgrammerActivity extends ActionBarActivity {
private Button button_plus_define;
private Button button_minus_define;
static private final int MAX_NO_EXER = 30;
private int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programmer);
final LinearLayout programmer_main_layout = (LinearLayout)findViewById(R.id.trainer_main);
//LinearLayout sequence_layout = (LinearLayout)findViewById(R.id.trainer_progcontent);
// Layout inflation for addition of the child view to the main view
final View adding_child = getLayoutInflater().
inflate(R.layout.sequence, programmer_main_layout, false);
programmer_main_layout.addView(adding_child);
// Plus button
button_plus_define = (Button)findViewById(R.id.button_plus);
button_plus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
i++;
programmer_main_layout.addView(adding_child);
/* Toast.makeText(getApplicationContext(),
"i is :" + i, Toast.LENGTH_LONG).show();*/
}
});
// minus button
button_minus_define = (Button)findViewById(R.id.button_minus);
button_minus_define.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
/*if(i>2) i--;
else {
Toast.makeText(getApplicationContext(),
"i is less than 2:" , Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(),
"i is :" +i , 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_programmer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the app crashes due to having
programmer_main_layout.addView(adding_child);
inside the + button
and here is the main error:
30799-30799/ca.cruxtec.cruxtectrainer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Every time you add a view as the child of another view,It should be a newer one.But you are adding the same reference "adding_child" each times.
I hope that you understood why the error "The specified child already has a parent" had occured.
I am writing an App that requires a layout to appear over another layout when the user presses a certain dropdown-menu style button. I am using Fragments and FragmentTransactions to do this. The Fragment does appear when I click the button (as it should) however when I click the button again, the fragment doesn't disappear.
Here is all relevant code:
MainActivity.java:
package com.example.tipquiz;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.R.anim;
public class MainActivity extends Activity implements OnRatingBarChangeListener {
// Testing Stuff to show the rating value, will need to use later for maths
RatingBar rb;
TextView tv;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
RelativeLayout dropDownLayout;
Boolean hasRotated = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownLayout = (RelativeLayout) findViewById(R.id.dropDownLayout);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void dropDown(View view){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
QuizFragment qf = new QuizFragment();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
if(hasRotated == false){
dropDownButton.setRotation(90);
dropDownLayout.setVisibility(View.VISIBLE);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
//qf.tv.setVisibility(View.VISIBLE);
hasRotated = true;
}else if(hasRotated == true){
dropDownButton.setRotation(0);
dropDownLayout.setVisibility(View.GONE);
//qf.tv.setVisibility(View.GONE);
hasRotated = false;
ft.hide(qf);
ft.remove(qf);
}
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
// final int numStars = ratingBar.getNumStars();
tv.setText(rating + "/5.0");
}
// http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html#.U7SZ5fldXm4
// http://custom-android-dn.blogspot.ca/2013/01/how-to-use-and-custom-ratingbar-in.html
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbcde3"
android:orientation="vertical" >
<GridLayout
android:id="#+id/gridLayout1"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:background="#e3e3e3"
android:columnCount="2"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="108dp"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_gravity="right|top"
android:layout_row="0"
android:background="#3fa9f5"
android:fontFamily="helvetica"
android:text="#string/settings_button"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</GridLayout>
<Space
android:layout_width="match_parent"
android:layout_height="12sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/gridLayout1"
android:layout_marginTop="24dp"
android:text="#string/rys"
android:textColor="#888888"
android:textSize="19sp" />
<RatingBar
android:id="#+id/ratingBar1"
style="#style/circleRatingBar"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:numStars="5"
android:rating="3.0"
android:stepSize="1.0" />
<ImageView
android:id="#+id/dropDownButton"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignBottom="#+id/ratingBar1"
android:layout_toRightOf="#+id/ratingBar1"
android:onClick="dropDown"
android:src="#drawable/ddb" />
<RelativeLayout
android:id="#+id/dropDownLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/textView2"
android:visibility="gone" >
<TextView
android:id="#+id/testTV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Testing dropdown" />
</RelativeLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ratingBar1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp"
android:text="#string/tipTitle"
android:textColor="#888888"
android:textSize="19sp" />
<FrameLayout
android:id="#+id/quizFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dropDownButton" />
</RelativeLayout>
Thanks in advance.
You forgot to .commit() your transaction when hasRotated is true.
Also, you're attempting to remove a newly created instance of QuizFragment -- not the existing one.
Use FragmentManager.findFragmentById() to get a reference to the existing Fragment, then call remove() with that reference.
Fragment quizFragment = fm.findFragmentById(R.id.quizFragment);
if (quizFragment != null) {
ft.remove(quizFragment);
ft.commit();
}
Also, it's preferable to just use if (!hasRotated) rather than if (hasRotated == false).
You're adding a new fragment everytime i.e
Just write the below statement outside the function.
QuizFragment qf = new QuizFragment();
and also you missed
ft.commit();
in the second case(hasRotated=true).
What your code is essentially doing is ,creating a new fragment and then adding it in the first case( hasRotated=false) and in the second case(hasRotated=true) its creating a new one and trying to remove this new fragment(which has not been added to the view) instead of trying to remove the previously created fragment.