This is my first time in creating an android app and this happens.
Any help is appreciated.
Here is my updated logcat after changing the manifest
04-10 16:00:17.154: E/AndroidRuntime(2480): FATAL EXCEPTION: main
04-10 16:00:17.154: E/AndroidRuntime(2480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miraapp/com.example.miraapp.MainActivity}: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.os.Looper.loop(Looper.java:137)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 16:00:17.154: E/AndroidRuntime(2480): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 16:00:17.154: E/AndroidRuntime(2480): at dalvik.system.NativeStart.main(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480): Caused by: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480): at com.example.miraapp.MainActivity.onCreate(MainActivity.java:30)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.Activity.performCreate(Activity.java:5104)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 16:00:17.154: E/AndroidRuntime(2480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 16:00:17.154: E/AndroidRuntime(2480): ... 11 more
Here is my code and i have another one for the new activity
package com.example.miraapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
Button button1;
EditText etResponse;
TextView tvIsConnected;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
}
private void button1Click()
{
startActivity(new Intent("com.example.miraapp.GUI"));
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
button1Click();
break;
case R.id.button2:
button2Click();
break;
}
}
private void button2Click()
{
}
};
btw, this is my activity_main
<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"
android:background="#000000"
tools:context="com.example.miraapp.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="131dp"
android:text="#string/START_fix"
android:onClick="button1Click"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:contentDescription="#string/title_fix"
android:src="#drawable/mira4" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="#string/CONNECT_fix" />
</RelativeLayout>
here is my updated androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miraapp"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17"
/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.miraapp.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="com.example.miraapp.GUI"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.miraapp.GUI" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Try this..
Remove android:name="Mira" from mainfest inside application like below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
And one more thing..
Change startActivity(new Intent("com.example.miraapp.GUI")); to startActivity(new Intent(MainActivity.this,GUI.class)); like below in button1Click() method
private void button1Click()
{
startActivity(new Intent(MainActivity.this,GUI.class));
}
EDIT
public class MainActivity extends Activity implements OnClickListener
And then
change button1.setOnClickListener((OnClickListener) this); as
button1.setOnClickListener(this);
Application
Base class for those who need to maintain global application state.
You can provide your own implementation by specifying its name in your
AndroidManifest.xml's application tag, which will cause that class
to be instantiated for you when the process for your
application/package is created.
So you have
<application
android:name="Mira
and the stacktrace says
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.miraapp.Mira" on path:
/data/app/com.example.miraapp-2.apk
You also say
i only have the class MainActivity
So get rid of android:name="Mira from application tag
Also you need to use Explicit intents
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
SO use
startActivity(new Intent(this,GUI.class);
Since you have explicit intents you can get rid of intent-filter
<intent-filter>
<action android:name="com.example.miraapp.GUI" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Edit:
Caused by: java.lang.ClassCastException:
com.example.miraapp.MainActivity cannot be cast to
android.view.View$OnClickListener
Change this
public class MainActivity extends Activity
to
public class MainActivity extends Activity implements OnClickListener
And then change this
button1.setOnClickListener((OnClickListener) this);
// this refers to the Activity. but you cast it to (OnClickListener) this
to
button1.setOnClickListener(this);
You need your class to implement interface OnClickListener
So you need to have button1.setOnClickListener(this);
MainActivity.java
package com.example.miraapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button button1;
EditText etResponse;
TextView tvIsConnected;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
private void button1Click() {
startActivity(new Intent(this, GUI.class));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
button1Click();
break;
case R.id.button2:
button2Click();
break;
}
}
private void button2Click() {
}
};
manifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.miraapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.miraapp.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="com.example.miraapp.GUI"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
GUI.java
package com.example.miraapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class GUI extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(), "hello", 1000).show();
}
}
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="94dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="44dp"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="46dp"
android:layout_marginRight="34dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
Related
I've made an app that allows user to login but everytime I try to run, my App is crashing constantly.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mertino11.ourapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_account.xml (XML for user logged in succesfully)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_account"
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.mertino11.ourapplication.AccountActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Account Page"
android:ems="10"
android:id="#+id/editText"
android:textSize="22sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textStyle="normal|bold"
android:textAlignment="center" />
</RelativeLayout>
activity_main.xml (login page)
<?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"
android:baselineAligned="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailField"
android:hint="Email"
android:paddingTop="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordField"
android:hint="Password"
android:fontFamily="sans-serif"
android:paddingTop="20dp" />
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/loginBtn"
android:paddingTop="20dp" />
</LinearLayout>
Java --> Class: AccountActivity (Page when the user logged in successfully)
package com.example.mertino11.ourapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class AccountActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
}
}
Java --> Class: FireApp (Firebase settings I think)
package com.example.mertino11.ourapplication;
import android.app.Application;
import com.firebase.client.Firebase;
/**
* Created by Mertino11 on 10-Dec-16.
*/
public class FireApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Java --> Class: Main Activity (Back-end logging with account)
package com.example.mertino11.ourapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private EditText mEmailField;
private EditText mPasswordField;
private Button mLoginBtn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passwordField);
mLoginBtn = (Button) findViewById(R.id.loginBtn);
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, AccountActivity.class));
}
}
};
mLoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSignIn();
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void startSignIn() {
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
Error Message at Run:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mertino11.ourapplication, PID: 3060
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.AccountActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.example.mertino11.ourapplication.MainActivity$1.onAuthStateChanged(MainActivity.java:48)
at com.google.firebase.auth.FirebaseAuth$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.
Screenshot of the error:
http://i66.tinypic.com/51btrp.png
The scenario of the app:
Create account firebase Google (manually)
Opens app --> Sees login page
Logs in with accountdetails of Firebase
Goes to AccountActivity page
Note: I am a amateur/beginner with AndroidStudio.
The error is pretty self-explanatory.
You have missed adding the AccounActivity on your manifest...
<application
android:name=".FireApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".AccountActivity"
android:theme="#style/AppTheme" />
</application>
Add AccountActivity in your AndroidManifest.xml
<activity
android:name=".AccountActivity">
<intent-filter>
...
</intent-filter>
</activity>
Always carefully read logcat:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.AccountActivity}; have you declared this activity in your AndroidManifest.xml?
I thank you all for giving me the solution. However (If it is possible) do you guys know why my App is running AccountActivity first instead of my Mainactivity? (it should run the mainactivity first)?
add .AccountActivity in manifest file as
<activity
android:name=".AccountActivity">
</activity>
I am trying to implement login with Facebook on my Android app. I am using the following tutorial http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837. I have no errors in the code but when I try to access the activity where the login with Facebook button is located I get a run time error. It cannot load the activity.
Logcat
04-06 14:57:05.024 8470-8470/com.example.martin.ivebeenthere E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:830)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.martin.ivebeenthere.Login.onCreate(Login.java:34)
at android.app.Activity.performCreate(Activity.java:5047)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.access$700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: null
at com.facebook.internal.Validate.sdkInitialized(Validate.java:99)
at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735)
at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109)
at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:830)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.martin.ivebeenthere.Login.onCreate(Login.java:34)
at android.app.Activity.performCreate(Activity.java:5047)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.access$700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Login.java
package com.example.martin.ivebeenthere;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.http.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.table.TableOperationCallback;
import java.net.MalformedURLException;
public class Login extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
info = (TextView)findViewById(R.id.info);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}
#Override
public void onCancel() {
info.setText("Login attempt canceled.");
}
#Override
public void onError(FacebookException e) {
info.setText("Login attempt failed.");
}
});
}
public void onClickbtnFeed(View view)
{
startActivity(new Intent(Login.this, Feed.class));
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.martin.ivebeenthere">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Home"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.splash.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar" />
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
<activity
android:name=".Map"
android:label="#string/title_activity_map" />
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Feed"
android:label="#string/title_activity_feed"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".CheckinInfo"
android:label="#string/title_activity_checkin_info"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Account"
android:label="#string/title_activity_account"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".EditAccount"
android:label="#string/title_activity_edit_account"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Checkin"
android:label="#string/title_activity_checkin"
android:theme="#style/AppTheme.NoActionBar" />
</application>
</manifest>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.martin.ivebeenthere.Login">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_login" />
</android.support.design.widget.CoordinatorLayout>
content_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.martin.ivebeenthere.Login"
tools:showIn="#layout/activity_login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/logo"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="276dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="Username"
android:id="#+id/editText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:hint="Password"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button3"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:onClick="onClickbtnFeed"/>
<!--<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Login With Facebook"
android:id="#+id/button4"
android:background="#3b5998"
android:textColor="#ffffff"
android:layout_below="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:layout_marginTop="25dp" />-->
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="18sp"
/>
</RelativeLayout>
Update with below code.
activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.widget.LoginButton
android:id="#+id/fb_login_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
facebook:confirm_logout="false"
facebook:fetch_user_info="true" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textSize="16sp" />
</LinearLayout>
Login.java
import java.util.Arrays;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.TextView;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.UserInfoChangedCallback;
public class Login extends FragmentActivity {
private LoginButton loginBtn;
private TextView username;
private UiLifecycleHelper uiHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (TextView) findViewById(R.id.username);
loginBtn = (LoginButton) findViewById(R.id.fb_login_button);
loginBtn.setReadPermissions(Arrays.asList("email"));
loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
#Override
public void onUserInfoFetched(GraphUser user) {
if (user != null) {
username.setText("You are currently logged in as " + user.getName());
} else {
username.setText("You are not logged in.");
}
}
});
}
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.d("Login", "Facebook session opened.");
} else if (state.isClosed()) {
Log.d("Login", "Facebook session closed.");
}
}
};
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
uiHelper.onSaveInstanceState(savedState);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.martin.ivebeenthere"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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" >
<activity
android:name=".Login"
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="com.facebook.LoginActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/APP_ID" />
</application>
</manifest>
I'm trying to create my first Android App using php but I'm having some difficulties getting started.
When I run my code I get this error log:
12-07 19:34:22.095: D/AndroidRuntime(13777): Shutting down VM
12-07 19:34:22.095: W/dalvikvm(13777): threadid=1: thread exiting with uncaught exception (group=0x42300c08)
12-07 19:34:22.095: E/AndroidRuntime(13777): FATAL EXCEPTION: main
12-07 19:34:22.095: E/AndroidRuntime(13777): Process: com.example.parents_gps, PID: 13777
12-07 19:34:22.095: E/AndroidRuntime(13777): java.lang.NullPointerException
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.example.parents_gps.MainActivity$1.onClick(MainActivity.java:42)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.view.View.performClick(View.java:4633)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.view.View$PerformClick.run(View.java:19270)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Handler.handleCallback(Handler.java:733)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Looper.loop(Looper.java:146)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.app.ActivityThread.main(ActivityThread.java:5602)
12-07 19:34:22.095: E/AndroidRuntime(13777): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 19:34:22.095: E/AndroidRuntime(13777): at java.lang.reflect.Method.invoke(Method.java:515)
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-07 19:34:22.095: E/AndroidRuntime(13777): at dalvik.system.NativeStart.main(Native Method)
I think the problem is this "thread exiting with uncaught exception" part, I have no idea what the exception could be or what's causing it.
Here is my code:
MainActivity.java: package com.example.parents_gps;
package com.example.parents_gps;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String SERVER_ADDRESS = "http://210.119.84.108/Gps";
AsyncTask<Void, Integer, Void> myTask;
ArrayList<String> identity, password;
int dataSize = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginBtn, registerBtn;
final EditText id, pw;
loginBtn = (Button) findViewById(R.id.loginBtn);
registerBtn = (Button) findViewById(R.id.registerBtn);
id = (EditText) findViewById(R.id.id);
pw = (EditText) findViewById(R.id.pw);
loginBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
&& String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {
Toast.makeText(MainActivity.this, "로그인 완료", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, LoginSuccess.class);
startActivity(intent);
} else
Toast.makeText(MainActivity.this, "ID 혹은 P/W가 올바르지 않습니다.", Toast.LENGTH_SHORT).show();
myTask = new AsyncTask<Void, Integer, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(SERVER_ADDRESS + "/search_regpar.php");
url.openStream();
identity = getXmlDataList("searchresult_p.xml", "identity");
password = getXmlDataList("searchresult_p.xml", "password");
dataSize = identity.size();
Log.i("Test", String.valueOf(identity.get(0)));
Log.i("Test2", String.valueOf(id.getText()));
for (int i = 0; i < dataSize; i++)
Log.d("size", String.valueOf(identity.get(i)));
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return null;
}
private ArrayList<String> getXmlDataList(String filename, String str) {
String rss = SERVER_ADDRESS + "/";
ArrayList<String> ret = new ArrayList<String>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL server = new URL(rss + filename);
InputStream is = server.openStream();
xpp.setInput(is, "UTF-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals(str)) {
ret.add(xpp.nextText());
}
}
eventType = xpp.next();
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return ret;
}
};
myTask.execute();
}
});
registerBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
Here is my activity_main.xml and AndroidManifest.xml:
activity_main.xml:
<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:gravity="center"
android:orientation="vertical"
tools:context="com.example.parents_gps.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="어서오세요"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textSize="20sp" />
<EditText
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="xxx#mail.com"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="P/W"
android:textSize="20sp" />
<EditText
android:id="#+id/pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Password를 입력하세요"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="로그인"
android:textSize="20sp" />
<Button
android:id="#+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="회원가입"
android:textSize="20sp" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parents_gps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".InfoTraced"
android:label="#string/app_name" >
</activity>
<activity
android:name=".InfoParents"
android:label="#string/app_name" >
</activity>
<activity
android:name=".InfoEdit"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterTraced"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterParents"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoginSuccess"
android:label="#string/app_name" >
</activity>
<activity
android:name=".MapView"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterActivity"
android:label="#string/app_name" >
</activity>
<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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBJe2YzPGrINQM88R8yXpVisnC1sl1TpT8" />
</application>
</manifest>
It looks like you didn't initalize your 'identity' and 'password' ArrayLists. They are null when you compare it here:
if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
&& String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {
I am up to the "Start Another Activity" section of the Android tutorial and it simply won't work when I install and test it.
It compiles fine but running it breaks after I click the send button.
I am using the command line tools on Ubuntu 12.04 and installing to a real device, my Galaxy S5.
I am aware of logcat but haven't been able to get it working, it either shows no output at all, or gives a massive spam of output I can't keep up with. I would happily provide logcat information if I could manage to isolate my app's output and cut through everything else.
I have seen a lot of similar questions on this which leads me to believe the tutorial isn't very well written.
Here is "MyActivity.java"
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.view.View;
public class MyActivity extends Activity
{
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
/** Called when the activity is first created */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** 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);
}
}
Here is DisplayMessageActivity
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import com.example.myfirstapp.R;
public class DisplayMessageActivity extends ActionBarActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Get the message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.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);
}
}
Here is AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity
android:name="MyActivity"
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="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MyActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MyActivity"
/>
</activity>
</application>
</manifest>
EDIT: Here is Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
There was some code that I changed and took out as it said to add code in, but then would later show a "this is what your code should look like" and it was missing some of the original code. I also changed the manifest package names in the activities because at the beginning it uses com.example.myfirstapp but later uses com.mycompany.myfirstapp
Any help or advice on why this seemingly simple tutorial doesn't work is greatly appreciated.
EDIT:
Logcat output - (after pressing the send button)
I/Timeline( 3812): Timeline: Activity_launch_request id:com.example.myfirstapp time:74636924
D/AndroidRuntime( 3812): Shutting down VM
E/AndroidRuntime( 3812): FATAL EXCEPTION: main
E/AndroidRuntime( 3812): Process: com.example.myfirstapp, PID: 3812
E/AndroidRuntime( 3812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
E/AndroidRuntime( 3812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
E/AndroidRuntime( 3812): at android.app.ActivityThread.access$900(ActivityThread.java:172)
E/AndroidRuntime( 3812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
E/AndroidRuntime( 3812): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3812): at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime( 3812): at android.app.ActivityThread.main(ActivityThread.java:5834)
E/AndroidRuntime( 3812): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3812): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 3812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
E/AndroidRuntime( 3812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
E/AndroidRuntime( 3812): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
E/AndroidRuntime( 3812): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
E/AndroidRuntime( 3812): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:16)
E/AndroidRuntime( 3812): at android.app.Activity.performCreate(Activity.java:6221)
E/AndroidRuntime( 3812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
E/AndroidRuntime( 3812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
E/AndroidRuntime( 3812): ... 10 more
I/Process ( 3812): Sending signal. PID: 3812 SIG: 9
I/ActivityManager( 871): Process com.example.myfirstapp (pid 3812)(adj 13) has died(104,205)
Here's the working code :
MainActivity.class
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
}
public void Go(View v)
{
Log.d("check", "Pressed");
String value = et.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", value);
startActivity(intent);
}
}
SecondActivity.class
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.TextView;
public class SecondActivity extends ActionBarActivity {
TextView textv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);
String txt = getIntent().getStringExtra("key");
Log.d("check", "got : " +txt);
textv = (TextView) findViewById(R.id.textView1);
textv.setText(txt);
}
}
res/layout/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.example.testproject.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="67dp"
android:layout_marginTop="46dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="Go"
android:text="Button" />
</RelativeLayout>
res/layout/new_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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="74dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/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=".SecondActivity"></activity> // Registered the newly created activity
</application>
Output :
Logcat :
BY SEEING YOUR LOGCAT, I got there is a problem in your Theme.
You didn't declared AppTheme in AndroidManifest.xml file
android:theme="#style/AppTheme"
Try by This Sample Code I have made For You.
mainactivity.xml Code
<EditText
android:id="#+id/edtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="message"
/>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>
MainActivity.class
Button btnSend;
EditText edtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtMessage=(EditText)findViewById(R.id.edtMessage);
btnSend=(Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String Message=edtMessage.getText().toString();
Intent intent=new Intent(getApplicationContext(),Second.class);
intent.putExtra("Message", Message);
startActivity(intent);
}
});
}
second.xml
<TextView
android:id="#+id/tviMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
Second.class
TextView tviMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tviMessage=(TextView)findViewById(R.id.tviMessage);
Bundle extras = getIntent().getExtras();
String message = extras.getString("Message");
tviMessage.setText(message);
}
Manifesto.xml
<application
android:allowBackup="true"
android:icon="#drawable/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=".Second"
android:label="#string/app_name" >
</activity>
By using logcat with this command:
adb logcat | grep `adb shell ps | grep com.example.myfirstapp | cut -c10-15`
I was able to find the error causing the app to crash. It came from missing the line:
android:theme="#style/Theme.AppCompat.Light"
In the Manifest.xml file. This is required by the Activity bar compatibility class. This part was completely overlooked in the tutorial, presumably because IDEs do this part automatically.
Thank you to everybody for their help and suggestions, it helped me keep looking and trying things until I finally found my solution.
I tried to implement custom font on TextView using the TypeFace class like this:
package com.royal.bikers;
import android.app.Activity;
import android.os.Bundle;
public class AboutUs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TextView aboutUs1 = (TextView) findViewById(R.id.aboutUs1);
TextView aboutUs2 = (TextView) findViewById(R.id.aboutUs2);
TextView aboutUs3 = (TextView) findViewById(R.id.aboutUs3);
TextView aboutUs4 = (TextView) findViewById(R.id.aboutUs4);
TextView aboutUs5 = (TextView) findViewById(R.id.aboutUs5);
TextView aboutUsTitle = (TextView) findViewById(R.id.aboutUsTitle);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
aboutUs1.setTypeface(tf);
aboutUs2.setTypeface(tf);
aboutUs3.setTypeface(tf);
aboutUs4.setTypeface(tf);
aboutUs5.setTypeface(tf);
aboutUsTitle.setTypeface(tf);
}
}
But the app's crashing:
05-09 13:42:13.525: E/AndroidRuntime(24251): FATAL EXCEPTION: main
05-09 13:42:13.525: E/AndroidRuntime(24251): Process: com.royal.bikers, PID: 24251
05-09 13:42:13.525: E/AndroidRuntime(24251): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.royal.bikers/com.royal.bikers.AboutUs}: java.lang.RuntimeException: native typeface cannot be made
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.os.Looper.loop(Looper.java:136)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-09 13:42:13.525: E/AndroidRuntime(24251): at java.lang.reflect.Method.invoke(Native Method)
05-09 13:42:13.525: E/AndroidRuntime(24251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-09 13:42:13.525: E/AndroidRuntime(24251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-09 13:42:13.525: E/AndroidRuntime(24251): Caused by: java.lang.RuntimeException: native typeface cannot be made
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.graphics.Typeface.<init>(Typeface.java:175)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
05-09 13:42:13.525: E/AndroidRuntime(24251): at com.royal.bikers.AboutUs.onCreate(AboutUs.java:23)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.Activity.performCreate(Activity.java:5231)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-09 13:42:13.525: E/AndroidRuntime(24251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-09 13:42:13.525: E/AndroidRuntime(24251): ... 9 more
I had tried using another font called Usuzi and it worked fine, but I want to change it to Roboto-Thin and it isn't working withit.
Here's my AboutUs.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/aboutUs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_bg"
tools:context="com.royal.bikers.AboutUs"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/aboutUsTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp"
android:gravity="center_horizontal"
android:text="#string/aboutUs"
android:textColor="#000"
android:textSize="25sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="105dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="110dp"
android:background="#59FFFFFF" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/aboutUs1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="#string/aboutUs1"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/aboutUs2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:text="#string/aboutUs2"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/aboutUs3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:text="#string/aboutUs3"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/aboutUs4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:text="#string/aboutUs4"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="#+id/aboutUs5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="#string/aboutUs5"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here's the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.royal.bikers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.NoActionBar" >
<activity
android:name="com.royal.bikers.Splash"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.royal.bikers.Home"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.Login"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.Register"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.AboutUs"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.Search"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.RegisterEvent"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.Event"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.Account"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.royal.bikers.ChangePassword"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
</application>
</manifest>
Please suggest me if there's any other method except creating CustomTextView class which extends TextView as given here
Ok then try this also,may be its a stream problem
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
here is the answer just remove the fonts string and use only font name.
Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Thin.ttf");
Make custom fontloader
public class FontLoader {
private static Typeface quickSandTypeface = null;
private static Typeface quickSandTypefaceBold = null;
public static void loadFonts(Context context) {
quickSandTypeface = Typeface.createFromAsset(context.getAssets(), "quicksand_regular.ttf");
quickSandTypefaceBold = Typeface.createFromAsset(context.getAssets(), "quicksand_bold.ttf");
}
public static Typeface getQuickSandTypeface() {
return quickSandTypeface;
}
public static Typeface getQuickSandTypefaceBold() {
return quickSandTypefaceBold;
}
public static void setQuickSandTypeface(EditText... editTextViews) {
if (null != editTextViews && null != quickSandTypeface) {
for (int i = 0; i < editTextViews.length; i++) {
editTextViews[i].setTypeface(getQuickSandTypeface());
}
}
}
Use in your activity this way
FontLoader.setQuickSandTypeface(yourEditText)
Try this it ll help you.
Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/helvetica.ttf");
ttf file should be in small letter.