Android project - cannot be resolved or is not a field - java

I have been working on various Android apps for quite some time now and am trying to finalize them. One of these apps involves creating a text field with a button. Below the button and the text field is some text (aligned left) and a spinner(calendar) which aligned right. Here is my code...
MainActivity.java
package com.miller.lab2;
import android.R;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
Spinner spinner = (Spinner) findViewById(R.id.listofmonths);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.monthlist, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view) {
EditText input = (EditText) findViewById(R.id.text);
String string = input.getText().toString();
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
Resources res = getResources();
String[] monthlist = res.getStringArray(R.array.monthlist);
}
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.miller.lab2.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="#+id/text"
android:text="#string/enter_message" />
<TextView
style="#+style/CustomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toLeftOf="#+id/button"
android:text="#string/text"
android:textColor="#347C17" />
<!-- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:text="#string/text2" /> -->
<Spinner
android:id="#+id/monthlist"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<EditText
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_message"
android:inputType="text"
android:text="#string/text_field" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text"
android:layout_centerInParent="true"
android:text="#string/button" />
</RelativeLayout>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.miller.lab2.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
I'm getting the following errors in my MainActivty.java:
listofmonths cannot be resolved or is not a field
monthlist cannot be resolved or is not a field
main cannot be resolved or is not a field
action_settings cannot be resolved or is not a field
text cannot be resolved or is not a field
Any help would be greatly appreciated

You are importing the R class from android
Delete this import import android.R; and reimport the R class from your project

Related

ListView isn't working, it is not displaying the listView on the main page

my ListView isn't working, it is not displaying the listView on the main page. Any solutions?
Does it have to do with my layout file too? I will attach the layout file below too!
I was trying to make an adapter for the listview
It is just a simple one page application which I try to learn how to use listview. Did I do any wrong about the code? I was mainly following a youtube tutorial for this but still an error occur
MainActivity.java
package com.example.myapplication;
import android.content.res.Resources;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
FloatingActionButton fab;
ListView myListView;
String [] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Resources res = getResources();
myListView = (ListView) findViewById(R.id.myListView);
items = res.getStringArray(R.array.items);
ItemAdapter itemAdapter = new ItemAdapter(this, items);
myListView.setAdapter(itemAdapter);
// myListView.setAdapter(new ArrayAdapter<String>(this,R.layout.));
fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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_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);
}
}
ItemAdapter.java
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
//A class that are BaseAdapter but with another name call 'ItemAdapter'
//To pass values/data to ListView
public class ItemAdapter extends BaseAdapter {
LayoutInflater mInflater;
String[] items;
public ItemAdapter(Context c, String[] i){
items = i;
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //To initialize inflater
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int i) {
return items[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View v = mInflater.inflate(R.layout.my_listview_details,null);
TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
String name = items[i];
nameTextView.setText(name);
return v;
}
}
activity_main.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=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="Your Library"
app:titleTextColor="#fff" />
<include
android:id="#+id/include"
layout="#layout/content_main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/round_add_white_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
content_main.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_marginTop="?attr/actionBarSize"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/myListView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
my_listview_details.xml
<?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:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
Any solutions?
used this
Change Height and width 0dp to as below
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
Instead of using ItemAdapter a custom class. If you have very simple list then just use the built-in ArrayAdapter class to register the listview.
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
listView.setAdapter(adapter);
Resources res = getResources();
myListView = (ListView) findViewById(R.id.myListView);
items = res.getStringArray(R.array.items);
-- > Try to check items.length in toast or console. Check if it is showing data or not.

Populate a Spinner class using by entering the first letter of elements in an array

I am supposed to create an app to generate a drop down list of states, that starts with a letter entered by user. The app should have one text input field and one button ("search"). It should also have an empty drop-down list below.
The list of all US states should be stored in an array.
Once a button clicked:
if there is a single letter entered a drop down list is populated with the states names from the array that starts with an entered
letter. If none or more than one letter is entered, nothing should be done.
I'm so lost and confused on what to do at this point. I tried using a searchView but I'm not sure if that is even the right way to go, as it's not doing any type of filtering what so ever, if i click the button the array just shows up in the drop down, all 50 states no matter what is typed in the searchView. I'm almost wondering if I should switch to just having it be a textView... but either way, help please! (end of semester is near and my teacher though he says "ask for help" is not even bothering to respond to email... sigh) Here is my code:
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/button"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look for States below"
android:id="#+id/textView"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search States"
android:id="#+id/button"
android:layout_below="#+id/searchView"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:layout_below="#+id/textView"
android:layout_alignStart="#+id/button"
android:queryHint="Search" />
</RelativeLayout>
MainActivity.Java:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
SearchView searchView;
Button stateLook;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchView = (SearchView)findViewById(R.id.searchView);
stateLook = (Button) findViewById(R.id.button);
};
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
states.add("Alabama");
states.add("Alaska");
states.add("Arizona");
states.add("Arkansas");
states.add("California");
states.add("Colorado");
states.add("Connecticut");
states.add("Delaware");
states.add("Florida");
states.add("Georgia");
states.add("Hawaii");
states.add("Idaho");
states.add("Illinois");
states.add("India");
states.add("Iowa");
states.add("Kansas");
states.add("Kentucky");
states.add("Louisiana");
states.add("Maine");
states.add("Maryland");
states.add("Massachusetts");
states.add("Michigan");
states.add("Minnesota");
states.add("Mississippi");
states.add("Missouri");
states.add("Montana");
states.add("Nebraska");
states.add("Nevada");
states.add("New Hampshire");
states.add("New Jersey");
states.add("New Mexico");
states.add("New York");
states.add("North Carolina");
states.add("North Dakota");
states.add("Ohio");
states.add("Oklahoma");
states.add("Oregon");
states.add("Pennsylvania");
states.add("Rhode Island");
states.add("South Carolina");
states.add("South Dakota");
states.add("Tennessee");
states.add("Texas");
states.add("Utah");
states.add("Vermont");
states.add("Virgina");
states.add("Washington");
states.add("West Virgina");
states.add("Wisconsin");
states.add("Wyoming");
final
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
#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);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
Toast.makeText(this, "You Selected "+ myText.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Ok...than do something like have an edittext and a button. on clicking button fill your adapter with filtered data.
For Ex.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Edittext
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look for States below"
android:id="#+id/search"
android:maxLength="1"
android:textSize="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search States"
android:id="#+id/button"
android:layout_below="#+id/searchView"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/button"
/>
</LinearLayout>
In Java Code
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
//fill your list with only matching criteria by any query
}

android widget within a widget not working as intended

I use a FrameLayout as a custom list item. I have an onItemClickListener set up to the listView but my items each have a button within themselves, and while the button in them is clickable, the item itself is not and they're supposed to do different things. How do I make the item as well as the button within it both clickable?
Here's the code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, 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);
}
}
The custom listView item xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="#+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
The xml of the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="#string/AddProject"
android:id="#+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/noProjectsNotice"
android:id="#+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addBtn"
android:id="#+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
Add the following two attributes for any view you want to be clickable inside the listview:
android:focusable="false"
android:focusableInTouchMode="false"
That should solve most cases. If that is not enough, also add the following attribute to the root layout of your list view layout
android:descendantFocusability="blocksDescendants"

All three buttons lead to same activity?

I'm having an issue that I can't quite figure out. I've got all three of my buttons to take me to another activity, but the problem is that they all lead me to the activity that I only want my first (Easy) button to do.
fragment_main_menu.xml:
<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: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="com.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<requestFocus />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
</RelativeLayout>
MainMenu.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.EditText;
import android.os.Build;
public class MainMenu extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.app.whosesloganisthat.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main_menu); //xml file name which contains button
}
#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, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu,
container, false);
return rootView;
}
}
EasyLevelInfo.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.TextView;
import android.os.Build;
public class EasyLevelInfo extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenu.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.easy_level_info, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_easy_level_info,
container, false);
return rootView;
}
}
fragment_easy_level_info.xml:
<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: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="com.app.whosesloganisthat.EasyLevelInfo$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/youhaveselectedeasy"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thanks everyone!
Look in your fragment_main_menu.xml
layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
You have three buttons buttonHard, buttonEasy , buttonIntermediate . And these three buttons have attribute android:onClick = "sendMessage" This means if one of these buttons is clicked do sendMessage method . Lets look on that method.
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
This method starts Activity EasyLevelInfo . So if one of three buttons is clicked it will call this sendMessage method and open EasyLevelInfo Activity .

Why does my app crash in the emulator?

I just started programming a little password manager but every time I run it in the emulator, it crashes and I get the following error:
android.widget.TextView cannot be cast to android.widget.EditText
Is everyone seeing the mistake?
MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpassfield = (EditText) findViewById(R.id.masterpass);
masterpass = "password";
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString() == masterpass) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/masterpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/login" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="#string/masterpasstext"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Project -> Clean seems to be the only solution to me.
Trying cleaning your project a few times and then build it.
Try Project -> Clean and also check if there are any warnings in any .xml file. If there are any the R.class will not be generated correctly.

Categories

Resources