I am new at Android and I'm trying to turn this Activity into a Fragment but I do not know how to resolve the Errors.
When I change it to a Fragment I get a lot of red code setContentView, findViewById, and getMenuInflater. I understand that these are probably red as they are meant to be used for Activities but I'm not sure what they are meant to be changed to.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
public class LocationsFragment extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_demo);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
recyclerView =
(RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter();
recyclerView.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.menu_card_demo, 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);
}
}
setContentView()
There is no method like this in Fragments. Instead, you override onCreateView() and return some view from it. You can use the passed-in LayoutInflater to inflate the layout id you'd normally pass to setContentView(), and then return that.
findViewById()
Fragments do not have a findViewById() method. However, any time after onCreateView() returns, you can instead use getView().findViewById(). Inside onCreateView(), if you named your inflated view root you could call root.findViewById(). Prior to onCreateView(), there's no way to look up any views (since they haven't been inflated yet).
getMenuInflater()
Fragments use a different signature for onCreateOptionsMenu(). It looks like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// ...
}
You can use the passed-in MenuInflater instead of calling getMenuInflater().
Put that all together, and this is what you'd have:
public class LocationsFragment extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_card_demo, container, false);
recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter();
recyclerView.setAdapter(adapter);
return root;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_card_demo, menu);
}
#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);
}
}
Related
I am joining two activity through intent
package com.smartcodeone.newapp1;
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.Button;
public class MainActivity extends ActionBarActivity {
public static final String STRING_VAR = "com.smartcodeone.newapp1.HELLO_WORLD";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnMsg = (Button) findViewById(R.id.btnMsg);
btnMsg.setOnClickListener(new View.OnClickListener(){
//when user click's this function will be called
public void onClick(View v){
Intent intentvar = new Intent(getApplicationContext(),Main2Activity.class);
intentvar.putExtra(STRING_VAR,"Hello World"); //this is used to pass data to next intent
startActivity(intentvar);
}
});
}
private int findViewId(int btnMsg) {
return 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.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 android.support.v4.app.FragmentManager getSupportFragmentManager() {
return null;
}
}
It might have to do with Android Studio. Try cleaning your project and then rebuilding. If that doesn't work go to File -> Invalidate Caches/Restart ...
I get those issues sometimes as well. Let me know if that works. I tried your code and it works fine.
The issue seems to be that you defined a method named: findViewById(int button) that always return 0.
Use the Activity method instead of your own:
this.findViewById(int resourceId)
Good luck!
It seems you are implementing your own findviewbyid(). I don't know if you intend to do so.
Try removing
private int findviewbyid(int btnMsg) {
}
The ActionBarActivity's findviewbyid should resolve your button from your layout file.
I am trying to build a Translucent Navigation bar for my android app. I separately tried developing the task bar and then had intend to include it in my application.
I am not allowed to upload images yet (as I don't have the reputation), therefore here's an example of what I am trying to build for my APP:
Image Slider example
I am getting errors in the MainActivity.java file.
I get an Error in the following line of code :
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close)
Error :
Error:(48, 79) error: cannot find symbol variable navigation_drawer_open
Error:(48, 112) error: cannot find symbol variable navigation_drawer_close
This is the entire code of MainActivity.java file:
package sha.testing_sidebar;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
String TITLES [] = {"Home","Events","Mail","Blog","Attending"};
int ICONS [] = {R.drawable.ic_home,R.drawable.ericsson_2_png,R.drawable.ic_mqil_hdpi,R.drawable.ic_blog_2,R.drawable.ic_attending};
String NAME = "Sharang Bharadwaj";
String EMAIL = "sha.bh91#gmail.com";
int PROFILE = R.drawable.sha1;
private Toolbar toolbar;
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
DrawerLayout Drawer;
ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
mAdapter = new sha.testing_sidebar.MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//actions upon opening slider
//presently nothing
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
//actions upon closing slider
//presently nothing
}
};
//Drawer Toggle Object made
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
#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);
}
}
The error is
Add the following in your strings.xml file
<string name="navigation_drawer_open">Drawer Open</string>
<string name="navigation_drawer_close">Drawer Closed</string>
It should work after that.
Just copy/paste this in the strings file
<string name="navigation_drawer_open">Drawer Open</string>
<string name="navigation_drawer_close">Drawer Closed</string>
they might be there already, if true, both will be underlined in red.
if so erase the existing ones and copy/paste the same two lines.
i'm starting to learn how to make android application by following the tutorial on their developer page. I'm trying to make a new DisplayMessageActivity class.
this is the tutorial https://developer.android.com/training/basics/firstapp/starting-activity.html#CreateActivity
My DisplayMessageActivity.java file contains by default this:
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
#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);
}
}
while on their website it sais it should contain this :
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
}
}
I have updated my eclipse to the latest version of ADT plugin and i still get this. When I try to paste their code to my DisplayMessageActivity.java file i get these errors:
activity_display_message cannot be resolved or is not a field
The method add(int, Fragment) in the type FragmentTransaction is not
applicable for the arguments (int,
DisplayMessageActivity.PlaceholderFragment)
fragment_display_message cannot be resolved or is not a field
Both of your references in the code should be to R.layout.activity_display_message, although I do not get any errors there (make sure you followed all the steps in the tutorial). But I am following the same tutorial and am getting the same error with the add method and cannot figure out why.
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
For practice I make the example "Building your first app" on https://developer.android.com/training/basics/firstapp/index.html
Now I have problem with Intents. Eclipse say "Unreachable Code" I have already imported the class Intent (import android.content.Intent;)
Intent intent = getIntent();
Here is the code:
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#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;
// 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) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
It can't be reached because you return in the line before.
return true;
// Get the message from the intent
**Intent intent = getIntent();**
Remove the return statement (make it the last thing you do in that method) or make it conditional.
Do it like this:
#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);
// 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);
return true;
}
Update your onCreateOptionsMenu method like this, return must be the last line of code you write in any Method. Post return statement the method does not execute anything.
#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);
// 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);
// last statement for any method
return true;
}