Errors in created activity help (android) - java

I keep getting errors in code below, specifically the two pieces surrounded by asterisks (R.id.action_settings and fragment_display_message,)
package com.example.completelypointlessapp;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.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
setContentView(textView);
}
#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_display_message,**
container, false);
return rootView;
}
}
}
The errors state:"fragment_display_message cannot be resolved or is not a field" and "action_settings cannot be resolved or is not a field"
Can anyone help resolve this?
(Apology for certain incorrect formatting, I am new to this site)

View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
Do you have fragment_display_message.xml layout file ?
if (id == R.id.action_settings)
Do you have the custom menu file for ActionBar ?
Example -
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never"
>
</item>
</menu>
If answer to both is NO, then error is obvious.

Related

Selected tab's color in Bottom Navigation View remain unchanged

I'm adding a BottomNavigationView to a project, and I would like to have a different text (and icon tint) color for the selected tab (to achieve greying out non-selected tabs effect). I used a color selector resource file with android:state_checked="true" and it worked fine until I implemented the BottomNavigationView.OnNavigationItemSelectedListener to one of my Fragment class. After adding that Listener the tab which was selected by default, remains highlighted even if I select a different tab. I tried different attributes to the state selector but nothing is helping.
Here is how I add the view to my layout:
<android.support.design.widget.BottomNavigationView
android:id="#+id/profileNavBar"
android:layout_alignParentTop="true"
android:background="#color/colorLavander"
app:itemTextColor="#color/nav_item_color"
app:itemIconTint="#color/nav_item_color"
app:menu="#menu/profile_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.BottomNavigationView>
Here is my color selector .xml file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorWhite" android:state_checked="true" />
<item android:color="#color/colorInactive" />
</selector>
And here is my Fragment class:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.home.com.gvwrev.R;
public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
BottomNavigationView bottomNavigationView = view.findViewById(R.id.profileNavBar);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
displayRevFragment(new RecordRevFragment());
}
public void displayRevFragment(Fragment fragment) {
getFragmentManager()
.beginTransaction()
.replace(R.id.revenueContainer, fragment)
.commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
}
return false;
}
}
Please help.
The problem is that you should return true when an item is selected in onNavigationItemSelected method. Try this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
return true;
}
return false;
}

My ActionBar icon isn't clickable

I'm having some issues with this icon, the thing is that I can do an onLongPress then it shows a toast with the text of this item.
My menu "new_offer_menu.xml"
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
android:showAsAction="ifRoom" />
</menu>
This icon isn't in all of my fragments, but when I go to the correct fragment (where it has to be shown), when I click it doesn't do nothing. In my ListaProductosFragment.java I have :
package info.androidhive.slidingmenu;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ListaProductosFragment extends ListFragment {
private List<ListViewItem> mItems; // ListView items list
private Menu optionsMenu;
public ListaProductosFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// initialize the items list
mItems = new ArrayList<ListViewItem>();
Resources resources = getResources();
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.levadura_oferta), getString(R.string.youtube), getString(R.string.youtube_precio), getString(R.string.youtube_descuento), getString(R.string.youtube_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.sopa_oferta), getString(R.string.bebo), getString(R.string.bebo_precio), getString(R.string.bebo_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento), getString(R.string.aim_data)));
mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapterListaProductos(getActivity(), mItems));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// remove the dividers from the ListView of the ListFragment
getListView().setDivider(null);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// retrieve theListView item
ListViewItem item = mItems.get(position);
// do something
Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.new_offer_menu,menu );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.newOffer:
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), TipusNouProducte.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I've created a toast to see if the problem is the Intent but it doesn't appears anyways, it only do something when I do a LongPress on this item.
What I'm doing wrong?
I believe you should change
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
android:showAsAction="ifRoom" />
</menu>
to
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourAppName="http://schemas.android.com/apk/res-auto"> <--
<item android:id="#+id/newOffer"
android:icon="#drawable/ic_action_new"
android:title="#string/nueva_lista"
android:alphabeticShortcut="r"
android:orderInCategory="200"
--> yourAppName:showAsAction="ifRoom" />
</menu>
The LongPress behavior, if I'm not mistaken, is a regular behaviour where the system explains to the user the meaning of the menuItem, getting the string from the title tag
android:title="#string/nueva_lista"
You are not managing it directly, it's Android itself that can manage it, giving you the wrong feeling.

Cannot get google tutorial to work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I just starting learning to develop an android app and for that i followed google's basic tutorial.
In the tutorial there is simply a textbox and a button. On pressing the button it takes the text from the textbox and displays it in another activity. But somehow the button isn't working for me.
Here is the code for the fragment_main.xml, MainActivity.java and DisplayMessageActivity.java. I just want to know if i am doing something wrong or if i am missing something.
fragmant_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
onClick="sendMessage" />
</LinearLayout>
MainActivity.java:
package com.example.first;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* 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, container,
false);
return rootView;
}
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity.java
package com.example.first;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.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
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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_display_message,
container, false);
return rootView;
}
}
}
EDIT **
In your fragment_main.xml file the the last line of your Button tag should be : android:onClick="sendMessage" instead of onClick="sendMessage"

setOnClickListener(this) crashes my app

I have a problem with btn.setOnClickListener(this). I have a simple project that works good, but when I add an action to the button I get the message:
unfortunatelay (app name) has stopped
When I comment btn.setOnClickListener(this) out the project works without any problem.
Here is my code:
package com.example.hello;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final Context PlaceholderFragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener {
Button btn;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
btn=(Button)rootView.findViewById(R.id.Entrer);
btn.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// here is the problem error: no eclosing istance of the type MainActivityis accessible in scope
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
}
My layout is as follows:
<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.example.hello.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="190dp"
android:text="#string/hello_world"
android:textSize="16dp" />
<Button
android:id="#+id/Entrer"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:text="Entrer" />
</RelativeLayout>
I dont know if I have simple things that are not go correctly. I'm new to Android programming. Thank you!
btn is null, so when you set onClickListener you get NullPointerException. It's null because it's located in fragment_main.xml, not activity_main.xml. You should move this code from onCreate()
btn = (Button) findViewById(R.id.Entrer);
btn.setOnClickListener(this);
to PlaceholderFragment() as follows:
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, container,
false);
btn=(Button) rootView.findViewById(R.id.Entrer);
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
}
});
return rootView;
}
}
btn=(Button)findViewById(R.id.Entrer);
btn.setOnClickListener(this);
Most likely, findViewById above is returning null. btn.setOnClickListener is crashing on a null pointer exception. (Because btn is null).
It is extremely likely, since you are using the default fragment structure created by the Eclipse solution, that the Button is really in the fragment layout file (fragment_main.xml) and not in activity_main.xml. (Hence, the button is not in the Activity view, it's in the fragment view).
If that's the case, then move the above two lines into the onCreateView method of the PlaceholderFragment.

No view found for id for fragment PlaceholderFragment

I created a Main activity, which sends me to an ActiviteResultats activity.
My goal is now just to show a message in this activity (Message who come from the main).
I followed the Starting Another Activity tutorial.
The problem is, when I start the ActiviteResultats activity, I have an error:
"no view found for id 0x7f080000 ........... for fragment PlaceholderFragment"
... and the app shuts down.
I don't know yet how to use fragments, and the tutorial says I don't have to use it for this example.
Where am I going wrong?
My code :
package com.example.surveyor;
import android.app.Activity;
import android.app.Fragment;
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;
public class ActiviteResultats extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activite_resultats);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
String message = "TODO";
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activite_resultats, 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_activite_resultats, container, false);
return rootView;
}
}
}
I had the same problem. Just remove the part:
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
That solved it for me. Hope that helps!
A quick look over the tutorial shows that most of the Fragment code is removed in a later step. You could remove all the code related to the fragments, along with the bit about the options menu if you want to clear out all distractions. It's likely to solve your problem and isn't vital to that tutorial
What you need to do is to set up intent in your main activity, and then get it here so it goes like follows:
under Main_Activity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
Intent i = new Intent(this,activiteResultats.class); // or getApplicationContext() <= for context
i.putExtras("TagOfMyMessage","MyMessageblablabla");
startActivity(i);
}
under ActivteResultats.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activite_resultats);
Intent i = getIntent();
String message = i.getStringExtras(); // or something like it
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
You just don't need to use fragments if you want a clear and easy tutorial i advice you to check this one : http://www.vogella.com/tutorials/AndroidIntent/article.html

Categories