At the beginning I notice that I'm novice at Android development.
I made application from http://developer.android.com/training/basics/firstapp/starting-activity.html and it worked well (second activity too). I want to make small change: in second activity I want to display "Your message:" text and below message from input from first activity. I did it my way and now when I tap Send button I have an application crash.
My second activity xml code:
<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.lislav.firstandroidapp.DisplayMessageActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_my_message"
android:textSize="40px"/>
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70px"/>
</RelativeLayout>
and second activity java code:
package com.example.lislav.firstandroidapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textViewMessage = (TextView) findViewById(R.id.message);
textViewMessage.setText(message);
setContentView(R.layout.activity_display_message);
}
#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);
}
}
I think it's something wrong with onCreate function.. But I don't know what. Sorry, if there are any obvious mistakes..
You must call findViewById method only after calling setContentView. Just put setContentView right after calling of super.onCreate.
By the way read about logcat tool. This is logger which you should use always facing any problem.
Related
I was following the tutorials online on Android Programming using Eclipse by New Boston and I am stuck at a particular point. So,everything was working fine, I had a button and a TextView in my GUI interface. Now, when i went to my java file and tried to locate it by using the R.Id..... it can't recognize the Button id or TextView Id declared in the XML file. Why am i getting this error? Can someone please correct my mistake. I am using Minimum SDK - API8- Android 2.2 ,Target SDK-API 13-Android 3.2 and Compiling with SDK- API 19- Android 4.4.
I checked the following article on Stack Overflow My Android application cannot find buttons declared in the XML file. As per the solution, I should delete the imports android.R from my java file and i dont have that import at all. So, i am kind of confused.
Main Activity.java
package com.example.androidprogram1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView display;
Button show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button)findViewById(R.id.button1);
display = (TextView)findViewById(R.id.textView1);
//Error message:- Create field Button1 in type id (Suggestions) /create Constant TextView1 in type id.....This is the error i get. It seems that the id of the button and textView is not adding to the R.java file
}
#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);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tvView"
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.androidprogram1.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="65dp"
android:layout_toRightOf="#+id/textView1"
android:text="#string/hello_world" />
</RelativeLayout>
Thanks a lot.!!
You need to instantiate the TextView and Button
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.button1);
display = (TextView) findViewById(R.id.textView1);
}
Edit: Your error message seems to be an Eclipse problem, rather than something you've done, unless you went in and made changes to R.java, which you shouldn't do. Save your project, close it, re-open and re-build it. Also, if you happen to have import android.R in your class, remove it.
I am writing a simple app that displays a fragment in the main activity. When I run the app, it displays two instances of the fragment on the screen. I have included the code below. By the way, when I remove FT.commit(); from the main activity class, only one fragment instance is displayed on the screen as it should. What is the problem here?
thanks.
Fragment layout XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Fragment Class
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class fragmentone extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragoner, container, false);
}
}
Main activity layout XML file:
<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">
<fragment
android:name="com.example.fragment.fragmentone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragone"
tools:layout="#layout/fragoner">
</fragment>
</RelativeLayout>
Main Activity Class
package com.example.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
FragmentTransaction FT = manager.beginTransaction();
fragmentone Fone = new fragmentone();
FT.add(R.id.fragone,Fone);
FT.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.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);
}
}
What is the problem here?
If you use the <fragment> tag, do not also use a FragmentTransaction to add the same fragment. Those are two separate techniques and should not be mixed.
Simply get rid of every line from onCreate() except the call to super.onCreate() and the setContentView() call, and stick with your static <fragment>.
Either remove the from your activity's layout view or stop using commit. You can use a as a container for your fragment. Add an id to your FrameLayout than use:
FragmentManger manager=getFragmentManager();
Fragment fragment=manager.findFragmentById(R.id.frameContainer);
I am implementing a simple page similar to any other app, a 'header image' and content below.
I've placed a wedhead.png file manually into all the drawable folders that I want to display.
The layout I wish to implement displays correctly on the graphical editor, but not on the emulator. The emulator comes out blank. Emulator is ARM CPU based.
Emulator screen -
Here's my code
XML File:
<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.wwmk1.MainActivity" >
<ImageView
android:id="#+id/img1"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="19dp"
android:contentDescription="header"
android:fadingEdge="vertical"
android:src="#drawable/wedhead"
android:visibility="visible" />
</RelativeLayout>
The Main activity java file
package com.example.wwmk1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView)findViewById(R.id.img1);
}
#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);
}
}
What place did I go wrong?
Please help. Thanks
I am following an online tutorial on Udemy. I created the default hello world app, but it would not load when I tried to run it! I decided to go to the next video and created a simple project that should display the text "Hello, World!" when a button is pressed, but I get this message on the emulator when I run the project:
Unfortunately Hello World has stopped.
I have been trying for hours searching multiple threads, but none seem to give me a solution. I am using the latest adt bundle (API 20) version 23. Here are the two files:
MainActivity.java
package com.ebook.helloworld;
import android.app.Activity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onBtnTouch(View v){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Hello, World!");
}
#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);
}
}
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="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.ebook.helloworld.MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:onClick="onBtnTouch"
android:text="Say Hi" />
<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="93dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Where did you set up the button?
You should put in the onCreate:
Button button1 = (Button)this.findViewById (R.id.button1);
And then you can:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do what you want to do...
}
});
I'm new to android and the problem feels simple, but I can't place it. I tried to look in logcat, but I can't follow.
Here is the main activity which loads just fine.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Intent intent;
Button newsButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
}
Here is the corresponding xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/newsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:onClick="#layout/news_activity"
android:text="News" />
<Button
android:id="#+id/contactButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
android:onClick="#layout/contact_activity" />
<Button
android:id="#+id/findUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Social Media"
android:onClick="#layout/social_activity" />
<Button
android:id="#+id/fundButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fundraising" />
</LinearLayout>
<TextView
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Send the Scots to Scotland"
android:textSize="24sp" />
</RelativeLayout>
The crash happens on the API 14 device I debug with. After clicking any one of my buttons to change to a new activity that looks like this :(which are all essentially the same as the one below)
package com.gconcode.scotstoscotland;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
public class NewsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#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);
}
}
Sorry I removed the logcat, I wasn't sure if I posted it right. Didn't want too cause the post to be inconvenient thanks for the help!
I think you misunderstand android:onClick. It looks like you are expecting it to load a certain layout file when you click the Button but that is not how it works. The name you give android:onClick should be the name of a function which doesn't return anything and accepts a View as its one and only parameter. It looks like you pulled that from the example in the docs with
public void sendMessage(View view) {
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
}
There are different ways you could do this. Since you already have the android:onClick and you have a method in your Activity, you could change that to look like
android:onClick="sendMessage"
then add a switch statement to that method to open the correct Activity. So something like
public void sendMessage(View view)
{
switch (view.getId()) // get the id of the View clicked
{
case (R.id.newsButton):
Intent intent = new Intent(this, NewsActivity.class);
startActivity(intent);
break;
case (R.id.contactButton):
// assuming ContactActivity.java is an Activity name
Intent intent = new Intent(view.getContext(), ContactActivity.class);
startActivity(intent);
break;
...
}
}
This answer shows a little different way to do the Intent so you cut down on some of the reusable code. If it looks too complicated then don't worry about it until you have a better understanding but just an idea.