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))) {
Related
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 implement a login Activity. In login Activity, I have to enter Mobile number and password and then click the Login button.
Here is my activity_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
tools:context="com.myayubo.LoginActivity"
android:fillViewport="false">
<RelativeLayout
android:id="#+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp">
<LinearLayout
android:id="#+id/LinearLayouttxtayubo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textAyubo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ayubo"
android:layout_gravity="center"
android:typeface="normal"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayouttxtexplorer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/LinearLayouttxtayubo"
android:orientation="vertical">
<TextView
android:id="#+id/textExplore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explore places nearby you"
android:layout_centerHorizontal="true"
android:typeface="serif"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:textSize="12dp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_marginTop="224dp"
android:layout_below="#+id/RelativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:id="#+id/mobileNo"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Mobile Number"
android:textColorHint="#FFFFFF"
android:gravity="center"
android:inputType="phone"
android:layout_gravity="center"
android:background="#drawable/shape"
android:layout_marginTop="10dp"
android:textSize="15dp"
>
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Password"
android:textColorHint="#FFFFFF"
android:inputType="textPassword"
android:layout_gravity="center"
android:background="#drawable/shape"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="15dp"/>
<Button
android:id="#+id/loginBtn"
android:text="Login"
android:layout_width="260dp"
android:layout_height="38dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:alpha="0.5" />
<Button
android:id="#+id/fbbtn"
android:text="Connect with Facebook"
android:layout_width="250dp"
android:layout_height="26dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ADD8E6"
android:alpha="0.5"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fbbtn"
android:layout_marginTop="20dp"
android:text="or Sign Up"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
And Here is my LoginActivity.java
package com.myayubo;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.myayubo.services.MessageHandler;
import com.myayubo.services.ServiceHandler;
import org.json.JSONObject;
public class LoginActivity extends ActionBarActivity implements View.OnClickListener {
public final static String URL = "";
public static String Uid;
private EditText contact;
private EditText password;
private Button login;
private Button fbBtn;
private TextView signUp;
private boolean errorStatus;
private ServiceHandler sh = new ServiceHandler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
contact = (EditText) findViewById(R.id.mobileNo);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.loginBtn);
fbBtn = (Button) findViewById(R.id.fbbtn);
signUp = (TextView) findViewById(R.id.signUp);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!ServiceHandler.isOnline(getApplicationContext())){
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
new Thread(new Runnable() {
public void run() {
Looper.prepare();
String mobile = contact.getText().toString();
String passwrd = password.getText().toString();
if (mobile.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Contact Number",
getApplicationContext());
errorStatus = true;
}
});
;
}
if (passwrd.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Password",
getApplicationContext());
errorStatus = true;
}
});
;
}
String jsonStr = null;
if (!errorStatus) {
if (!ServiceHandler.isOnline(getApplicationContext())) {
MessageHandler.showMessage("No network connection",
getApplicationContext());
} else {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// notify user you are online
try{
runOnUiThread(new Runnable() {
public void run() {
}
});
;
jsonStr = sh.makeServiceCall(URL + "/" + mobile + "/"
+ passwrd, ServiceHandler.GET);
System.out.println(URL + "/" + mobile + "/"
+ passwrd);
}
catch (Exception e){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
});
;
}
}
if (jsonStr != null) {
String status = "";
String msg = "";
try {
JSONObject jsonObj = new JSONObject(jsonStr);
runOnUiThread(new Runnable() {
public void run() {
}
});
;
if (jsonObj != null
&& jsonObj.has("status")) {
status = jsonObj.getString("status");
msg = jsonObj.getString("message");
if(jsonObj.has("uid"))
Uid = jsonObj.getString("uid");
System.out.println(jsonObj);
if (status.equals("OK")) {
Intent myIntent = new Intent(
getBaseContext(),
ExtractMenu.class);
startActivityForResult(myIntent, 0);
} else if (status.equals("ERROR")) {
final String errorMsg = msg;
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
errorMsg,
getApplicationContext());
}
});
;
} else {
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
"Oops..! something wrong with the service. Please try again Later.",
getApplicationContext());
}
});
;
}
}
} catch (Exception e) {
System.out
.println("Creation of json object failed");
}
}
}
}
}).start();
}
});
signUp.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(this, sign_up.class);
startActivity(intent);
}
}
Looks like something is wrong here. Because, when I'm trying to run this code, my app crashes. It shows the following error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}: java.lang.NullPointerException
But, without intent for signUp button it works.
Here is my logcat.
11-05 10:36:58.359 27532-27532/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 27532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myayubo.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Please some1 tell me what to do with this error.
Thaks in advance.
-Edit -
Here is the Manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myayubo"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="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="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect
option is required to comply with the Google+ Sign-In developer policies
-->
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<activity
android:name=".Splash"
android:label="Ayubo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExtractMenu"
android:label="#string/title_activity_extract_menu" >
</activity>
<activity
android:name=".sign_up"
android:label="#string/title_activity_sign_up" >
</activity>
</application>
</manifest>
you are getting NPE because you are trying to add view which is not in your xml file
signUp = (TextView) findViewById(R.id.signUp);
there is no text view in your xml with this id. check it out if you remove this your code will run perfectly
NullPointerException Because there isn't any TextView with id signUp in activity_login.xml layout.
In your onCreate Method
signUp = (TextView) findViewById(R.id.signUp);
But as your activity_login.xml does not contain TextView with id signUp it return null,
And again you try to set listener on that TextView which is null so NullPointerException
signUp.setOnClickListener(this);
Looks like you fogot to set id here,
<TextView
android:id="#+id/text2" //android:id="#+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fbbtn"
android:layout_marginTop="20dp"
android:text="or Sign Up"
android:layout_gravity="center"/>
I'm getting a nullPointerException when I try to access my ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
my recipientsActivity is this
package com.ahmetyuva.ribbit;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import java.util.List;
public class RecipientsActivity extends ListActivity{
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipients);
getActionBar().setDisplayHomeAsUpEnabled(true);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
#Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> friends, ParseException e) {
if(e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
} ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getListView().getContext(),
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
}
else{
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipients, menu);
mSendMenuItem = menu.getItem(0);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send:
return true;
}
/* int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
*/
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (l.getCheckedItemCount() > 0) {
mSendMenuItem.setVisible(true);
} else {
mSendMenuItem.setVisible(false);
}
}
}
my error log is here
04-13 11:19:05.148 1956-1956/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmetyuva.ribbit/com.ahmetyuva.ribbit.RecipientsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.ahmetyuva.ribbit.RecipientsActivity.onCreate(RecipientsActivity.java:40) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)
menu_recipients xml is here
<menu 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"
tools:context="com.ahmetyuva.ribbit.RecipientsActivity">
<item android:id="#+id/action_send"
android:title="Send"
android:orderInCategory="100"
app:showAsAction="always"
android:visible="true"
android:icon="#drawable/ic_action_send_now"
/>
</menu>
activity_recipients xml is here
<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"
tools:context="com.ahmetyuva.ribbit.RecipientsActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty_recipients_list_message"
/>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ahmetyuva.ribbit" >
<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-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission
android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE" />
<application
android:name=".RibbitApplication"
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=".LoginActivity"
android:label="#string/title_activity_login"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignUpActivity"
android:label="#string/title_activity_sign_up"
android:parentActivityName=".LoginActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".EditFriendsActivity"
android:label="#string/title_activity_edit_friends"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.ahmetyuva.ribbit" />
</intent-filter>
</receiver>
<activity
android:name=".RecipientsActivity"
android:label="#string/title_activity_recipients"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
</application>
</manifest>
Try to use
getSupportedActionBar() instead of getActionBar()
First of all, always do this to avoid this kind of error :
ActionBar ab = getSupportActionBar();
if (ab == null) {
Log.d("test", "ab null");
}
else {
Log.d("test", "ab not null");
ab.setDisplayHomeAsUpEnabled(true);
}
Then, what is in the logcat with the tag "test" ?
Make sure that you actually have an ActionBar, if for example, your theme is:
Theme.Light.NoActionBar, you won't have an action bar.
So, make sure your theme has and Actionbar
EDIT:
Try with the following theme:
<style name="AppTheme" parent="Theme.AppCompat">
If that doesn't work, You should consider changing to fragments, as suggested in this SO Answer
For both activities,
in your onCreate the next line after setContentView( ... )
add the following :
getActionBar().setDisplayHomeAsUpEnabled(true);
in the manifest for both activities add
android:theme="#android:style/Theme.Holo.Light"
in the tag directly after
android:screenOrientation="portrait"
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.
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>