I am not able to set specifc locale on button press. when the button is press of the specific lang. i want the button to redirect to the mainmenu page and then render the basic structure there which consist of five buttons.
Do i need to have the languages installed in the emulator? they are not present in the emulator
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocale);
Button eng =(Button)findViewById(R.id.en);
eng.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Locale local=new Locale("en");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});
Button dar =(Button)findViewById(R.id.dr);
dar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Locale local=new Locale("fa");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});
Button pas =(Button)findViewById(R.id.ps);
pas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Locale local=new Locale("ps");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});
}
then redirect to Mainmenu activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GlobalClass glb=(GlobalClass)getApplicationContext();
Locale loc =glb.getLocale();
Locale.setDefault(loc);
Configuration config =new Configuration();
config.locale=loc;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main_menu);
}
mainmenu.xml page
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.sepia2.myapplication.Main3Activity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link1"
android:id="#+id/link1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link2"
android:id="#+id/link2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link3"
android:id="#+id/link3"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link4"
android:id="#+id/link4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link5"
android:id="#+id/link5"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
My values-fa
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Darri HajjApp</string>
<string name="link1">darri link1</string>
<string name="link2">darri link2</string>
<string name="link3">darri link3</string>
<string name="link4">darri link4</string>
<string name="link5">darri link5</string>
</resources>
My values-ps
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">pashto HajjApp</string>
<string name="link1">pashto link1</string>
<string name="link2">pashto link2</string>
<string name="link3">pashto link3</string>
<string name="link4">pashto link4</string>
<string name="link5">pashto link5</string>
</resources>
Global Class
public class GlobalClass extends Application {
private Locale _slocale;
public void setLocale(Locale loc)
{
_slocale=loc;
}
public Locale getLocale()
{
return _slocale;
}
}
This is what I missed in xml file (above is a complete code that works)
just change instead of
android:text="Link1"
it should be from resource
android:text="#string/link1"
And Don't forget to add GlobalClass in AndroidManifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.example.sepia2.myapplication.GlobalClass">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".settlocale" />
<activity android:name=".Main3Activity" />
<activity android:name=".MainMenu"></activity>
</application>
Related
We are having a resource error when we try to run our code.
package com.example.searchtest;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class GoogleSearchIntentActivity extends Activity {
private EditText editTextInput;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
}
public void onSearchClick(View v)
{
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">
<edittext
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestfocus>
</requestfocus></edittext>
<button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</button></linearlayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchtest">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".GoogleSearchIntentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
these are our mainActivity.java, activity_main.xml, and AndroidManifest.xml codes from our app.
The error is within our public class GoogleSearchIntentActivity extends Activity
We are high school students trying to create an image search app for our computer science capstone class, we have been trying to fix this error without success, and our teacher is completely clueless.
Components in your xml file must have a first capitalized letter.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp">
<EditText
android:id="#+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search text">
<requestFocus>
</requestFocus></EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_gravity="center"
android:onclick="onSearchClick"
android:layout_margintop="10dp">
</Button></LinearLayout>
This is App ID I have added it in Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.rishavgmail.coder.carescountrysactionresourcesexigencyservices">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".UserLoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity" />
<activity android:name=".UserLoginPage" />
<activity android:name=".UserRegistration" />
<activity android:name=".AuthorizationLoginPage" />
<activity android:name=".OtpStepOne" />
<activity android:name=".VerifyPhone" />
<activity android:name=".AuthorityRegistration" />
<activity android:name=".AuthorityHome" />
<activity android:name=".AuthReg" />
<activity android:name=".AuthLoginPage" />
<activity android:name=".SplashScreen"></activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-*******381606112~********91"/>
</application>
</manifest>
My AdMob AdUnitId in the string.xml file
<resources>
<string name="app_name">C A R E S : Country\'s Action Resources Exigency Services</string>
<string name="open">Open</string>
<string name="close">Close</string>
<string name="event_name">Event/Party/Restaurant/Other Name </string>
<string name="event_location">Event Address</string>
<string-array name="events">
<item>SELECT AN EVENT</item>
<item>PARTY</item>
<item>RESTAURANT</item>
<item>OTHER EVENTS</item>
</string-array>
<string name="foods">Foods</string>
<string name="btn_send">Inform Authority</string>
<string name="copyright">
Developed by 16Bit Coders
</string>
<string name="userphone">
Your Phone Number
</string>
<string name="user_login_button">
LOGIN AS USER
</string>
<string name="authority_login_button">
LOGIN AS AUTHORITY
</string>
<string name="user_email">Email</string>
<string name="user_password">Password</string>
<string name="confirm_password">Confirm Password</string>
<string name="login">LOGIN</string>
<string name="register">REGISTER</string>
<string name="fname">First Name</string>
<string name="lname">Last Name</string>
<string name="welcome_authority">Authority Login</string>
<string name="send_otp">SEND OTP</string>
<string name="verify_otp">Verify OTP</string>
<string name="enter_otp">Enter OTP</string>
<string name="enter_aadhaar">Aadhar Card Number</string>
<string-array name="gender">
<item>SELECT GENDER</item>
<item>MALE</item>
<item>FEMALE</item>
</string-array>
<string-array name="location">
<item>91</item>
</string-array>
<string name="thanks">Thank You So Much</string>
<string name="thanks2">You Just Take a Part of Helping Needy People</string>
<string name="hero">Awesome.. You are a real Hero !!</string>
<string-array name="ngo_list">
<item>Jeevan Life Seva Sangstha</item>
<item>Astha Foundation</item>
<item>AIWA NGO India</item>
<item>All India Human Rights Bureau</item>
<item>Chain Foundation</item>
<item>Helping Hand Foundation</item>
<item>Human Wellfair Foundation</item>
</string-array>
<string name="banner_ad">ca-app-pub-*******381606112/*******732</string>
</resources>
I have called adunitId in the xml file from string.xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
app:srcCompat="#drawable/logo"
tools:layout_editor_absoluteX="174dp"
tools:layout_editor_absoluteY="54dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserLoginActivity">
<Button
android:id="#+id/user_login_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="8dp"
android:background="#drawable/login_button_style"
android:elevation="10dp"
android:text="#string/user_login_button"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/authority_login_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:background="#drawable/login_button_style"
android:elevation="10dp"
android:text="#string/authority_login_button"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_login_button" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/copyright"
app:layout_constraintBottom_toTopOf="#+id/adView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="#+id/nestedScrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
And finally I setup java code to show ads in app
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.firebase.auth.FirebaseAuth;
public class UserLoginActivity extends AppCompatActivity {
private Button btnLogin,btnAuthLogin;
private FirebaseAuth mAuth;
private AdView adview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
adview = findViewById(R.id.adView2);
AdRequest adRequest = new AdRequest.Builder().build();
adview.loadAd(adRequest);
btnLogin = findViewById(R.id.user_login_button);
btnAuthLogin = findViewById(R.id.authority_login_button);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UserLoginActivity.this,UserLoginPage.class));
}
});
btnAuthLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent =new Intent(UserLoginActivity.this,AuthLoginPage.class);
startActivity(intent);
}
});
}
}
What can I do now, I have researched on google, I have reviewed StackOverflow posts as well but I can't find any solution, how can I solve it.
Even as Google AdMob policy I just added one single ad in one activity only, still it not showing ads
Thank you
It takes up to 72 hours for AdMob to approve your account and start showing ads. You can use test ads until then.
Test ad IDs
You have written every code but you no where initialize the Admob SDK. Initialize it as soon as the app launches using
MobileAds.initialize(getApplicationContext(), getString(R.string.YOUR_APP_ID));
i'm fairly new to coding but decided i would take the huge plunge into the ocean without knowing how to swim, anyway my button keeps failling to open my new activity when i click on it, instead it just crashes. when I run it on my emulator i get a message saying that it has stopped. I'm using a genymotion emulator and the darcula android studio version.
My main activity.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="wrap_content" 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"
android:background="#drawable/background_black">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unpressed_button"
android:background="#drawable/unpressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My second Activity.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/background_black"
tools:context="quirkykoders.flash_flash.Pressed_Button">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pressed_button"
android:background="#drawable/pressed"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="quirkykoders.flash_flash" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Pressed_Button"
android:label="#string/title_activity_pressed__button" >
<intent-filter>
<action android:name="android.intent.action.PRESSED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My java
package quirkykoders.flash_flash;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.unpressed_button);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent ("quirkykoders.flash_flash.Pressed_Button");
startActivity(intent);
}
}
);
}
}
Any help will be greatly appreciated
When you want to create A new Activity you have to send two parameters in the intent object First is currentActivity name and the second is CallingActivity name
Intent intent=new Intent(MainActivity.this,Pressed_Button.class);
startActivity(intent);
I am working with Microsoft Translator API in my app. I am unable to execute this code:
MainActivity:
public class MainActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
Translate.setClientId("My client");
Translate.setClientSecret("secret key");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Trans = (Button)findViewById(R.id.bTranslate);
Trans.setOnClickListener(this);
}
public void onClick(View v) {
//get the text entered
EditText input = (EditText)findViewById(R.id.etUserText);
TextView output = (EditText)findViewById(R.id.tvTranslatedText);
String In =input.getText().toString();
//String Out;
try {
String Out = Translate.execute(In, Language.AUTO_DETECT, Language.FRENCH);
input.setText(Out);
output.setText(Out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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" >
<EditText
android:id="#+id/etUserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tvTranslatedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etUserText"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/bTranslate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etUserText"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Translate"
android:onClick="trans"/>
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.translator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:permission="android.permission.LOCATION_HARDWARE">
<activity
android:name="com.example.translator.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
String In =input.getText().toString();
I think your problem is in the line above .. an object cannot contain capital letters .. so, change (In) into (in) .. worth the try
I'm already look for an answer in other questions, but i didn't found it or didn't see it, i'm grateful for any help thanks.
these are the errors shown in the Log Cat
E/Trace(1417): error opening trace file: No such file or directory (2)
E/AndroidRuntime(1417): java.lang.RuntimeException: Unable to start activity ComponentInfo{gabrielrojas.com/gabrielrojas.com.primeraventana}: android.view.InflateException: Binary XML file line #7: Error inflating class textview
E/AndroidRuntime(1417): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class textview
The androidmanifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".PruebaNumero3"
android:label="#string/title_activity_prueba_numero3" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".primeraventana"/>
<activity android:name=".segundaventana" />
</application>
</manifest>
PruebaNumero3.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".PruebaNumero3" />
<Button android:id="#+id/boton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Primera_Ventana"/>
<Button
android:id="#+id/boton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="#string/Segunda_Ventana" />
</RelativeLayout>
PruebaNumero3.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prueba_numero3);
// Visualizamos Los botones
Button boton1 = (Button) findViewById(R.id.boton1);
Button boton2 = (Button) findViewById(R.id.boton2);
// Fijamos los eventos que haran ir a las otras actividades
boton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PruebaNumero3.this, primeraventana.class);
startActivity(intent);
}} );
boton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PruebaNumero3.this, segundaventana.class);
startActivity(intent);
}} );}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_prueba_numero3, menu);
return true;
} } ;
primeraventana.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Escala_Uno"/>
<Button android:id="#+id/boton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Menú_Principal"/>
</LinearLayout>
primeraventana.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primeraventana);
TextView text = (TextView) findViewById(R.id.textview1);
Button boton3 = (Button) findViewById(R.id.boton3);
text.setText(R.string.Escala_Uno);
boton3.setText(R.string.Menú_Principal);
boton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
segundaventana.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Escala_Dos"/>
<Button android:id="#+id/boton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Menú_Principal"/>
</LinearLayout>
segundaventana.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primeraventana);
TextView text = (TextView) findViewById(R.id.textview2);
Button boton4 = (Button) findViewById(R.id.boton4);
text.setText(R.string.Escala_Dos);
boton4.setText(R.string.Menú_Principal);
boton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish ();
Check your primeraventana.xml layout file. You opened TextView tag with textview which is not existing. Change it as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Escala_Uno"/>
<Button android:id="#+id/boton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Menú_Principal"/>
</LinearLayout>
I don't know exactly what the problem is, but I do have a few things you can try:
1)
In primeraventana.xml your TextView should be spelled in capitals. Like so:
<TextView android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Escala_Uno"/>
2)
In segundaventana.java the String names can only be English letters and numbers. Remove the "ú" in:
boton4.setText(R.string.Menú_Principal);
Lastly, I declare OnClickListeners() like this:
button.setOnClickListener(new View.OnClickLister() {
public void onClick(View v) {
// You code here.
}
}
I don't know weather that makes any difference to not having the View. It just looks nicer. :)