I'm trying to develop basic facebook activty and The app will get username and display it in screen but I'm facing a kind of FATAL EXCEPTION: main error I have provided my complete set of codes starting with manifest file, class file, xml file and the Logcat error,your help is highly required
The below provided code is my manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.world.trialwithmugaputhagam">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplicatioon"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity android:name=".MainFragment" />
</application>
The below code is the class file I have used.In below code I have also assigned mTextDetails but when I hover the cursor it say the value is not assigned
import android.os.Bundle;
import android.app.Fragment;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends Fragment {
private TextView mTextDetails;
private CallbackManager mcallbackManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mcallbackManager=CallbackManager.Factory.create();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main,container,false);
}
private FacebookCallback<LoginResult> mcallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome "+ profile.getName());
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
};
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton. setReadPermissions("user_friends");
loginButton. setFragment(this);
loginButton.registerCallback(mcallbackManager, mcallback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mcallbackManager.onActivityResult(requestCode, resultCode, data);
}}
The below code is my XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.world.trialwithmugaputhagam.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Welcome"
android:id="#+id/text_details"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_above="#+id/login_button"/>
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_centerInParent="true"/></RelativeLayout>
Logcat error Sorry about the wrong alignment.
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.world.trialwithmugaputhagam/com.example.world.trialwithmugaputhagam.MainActivity}: java.lang.ClassCastException: com.example.world.trialwithmugaputhagam.MainActivity cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.world.trialwithmugaputhagam.MainActivity cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Because you are extending MainActivity from Fragment. Thus, your MainActivity is not actually an Activity but a Fragment. Extend it from Activity or subclasses of Activity.
Like this:
MainActivity extends AppCompatActivity
Edit 1: (Based on comments):
You haven't initialized mTextDetails;
try to do like this in Activity's onCreate:
mTextDetails = (TextView)findViewById(R.id.text_view_id);
Related
These are the sections of code mentioned.As I run these errors pop up in logcat file and the app crashes when opened.There are so many of them that I could not figure out where the actual mistake is.Please Help me out where there is need of modification and replacement as I am trying to build a splash screen.
logcat:
06-08 11:36:18.203 17265-17265/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elvero.telecom.voipapp, PID: 17265
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elvero.telecom.voipapp/com.elvero.telecom.voipapp.splash}: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.elvero.telecom.voipapp.splash.onCreate(splash.java:13)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x6
at android.content.res.TypedArray.getDimensionPixelOffset(TypedArray.java:533)
at android.view.View.<init>(View.java:3927)
at android.widget.ImageView.<init>(ImageView.java:139)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.elvero.telecom.voipapp.splash.onCreate(splash.java:13)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5717)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
06-08 11:36:22.207 17265-17265/? I/Process: Sending signal. PID: 17265 SIG: 9
MainActivity.java:
package com.elvero.telecom.voipapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.elvero.telecom.voipapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
splashactivity:
<?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:visibility="visible"
tools:context="com.elvero.telecom.voipapp.splash">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#android:color/holo_blue_light"
android:elevation="#android:dimen/dialog_min_width_minor"
android:scaleType="fitCenter"
android:src="#drawable/logo_elvero"/>
</RelativeLayout>
Splash.java:
package com.elvero.telecom.voipapp;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler handler=new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run(){
startActivity(new Intent(splash.this,MainActivity.class));
finish();
}
},1000);
}
}
error caused because code
android:elevation="#android:dimen/dialog_min_width_minor"
its better to give direct value to it like
android:elevation="5dp"
You can make separate Activity as a splash screen and use below code in onCreate() to show it for required seconds and then can move to your main landing activity.
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
HarmUtils.openActivity(SplashScreenActivity.this, LoginActivity.class, true);
}
}.start();
here 2000 is waiting time (2000 = 2 seconds) and second parameter is time interval which I set to 1 second.
You can specify your time as per your requirements.
Hope this will help you.
I'm new to android and trying to add Facebook login and catch the event after clicking the Logout button(using AccessTokenTracker) in android using facebook sdk using Android studio but i'm getting this error
here is the logcat,
// (please horizontal scroll for logcat)
01-12 00:47:15.306 12572-12572/com.example.arpit.facebooklogindemo E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.example.arpit.facebooklogindemo/com.example.arpit.facebooklogindemo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.arpit.facebooklogindemo.MainActivity.onResume(MainActivity.java:83)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
at android.app.Activity.performResume(Activity.java:5211)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Here is AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arpit.facebooklogindemo">
<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">
//from http://developers.facebook.com
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//from http://developers.facebook.com
<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" />
</application>
</manifest>
Here is activity_main.xml,
<?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: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.arpit.facebooklogindemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView2" />
//from http://developers.facebook.com
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_above="#+id/login_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" />
</RelativeLayout>
Here is MainActivity.java,
package com.example.arpit.facebooklogindemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginButton loginButton;
TextView textView;
AccessTokenTracker accessTokenTracker;
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if(profile != null){
fillTextView(profile);
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.d("find", String.valueOf(error));
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
if(currentAccessToken == null){
textView.setText("Logged out");
}
}
};
accessTokenTracker.startTracking();
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, mFacebookCallback);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onResume(){
super.onResume();
Profile profile = Profile.getCurrentProfile();
textView.setText(profile.getName());
}
#Override
protected void onDestroy(){
super.onDestroy();
accessTokenTracker.stopTracking();
}
private void fillTextView(Profile profile){
textView.setText(profile.getName());
}
}
Look at this line in your logs:
Caused by: java.lang.NullPointerException
at com.example.arpit.facebooklogindemo.MainActivity.onResume(MainActivity.java:83)
It is clearly mentioned that your app is crashing due to NullPointerException in onResume() function. One possible reason is you might be getting null profile details while calling profile.getName()
Make a null checking before setting text as below:
Profile profile = Profile.getCurrentProfile();
if(null != profile)
textView.setText(profile.getName());
Sometimes, you might get null or old profile details. In such case, you need to request for updated profile details. Below is samle code:
Profile profile = Profile.getCurrentProfile();
if(null != profile) {
new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if (currentProfile != null) {
// handle it
stopTracking();
}
}
}.startTracking();
} else {
textView.setText(profile.getName());
}
put a null check on your onResume before setting textView.
I'm trying to make an app that simply logs into facebook and then logs out. My app would compile and load however adding the line: loginButton.setFragment(this) causes a compilation which I have fixed by using the package android.support.v4.app.Fragment rather than android.app.Fragment. The issue is that now my app crashes whenever I try to run it.
This is my MainFragment.java
//MainFragment.java
//import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
/**
* A placeholder fragment containing a simple view.
*/
public class MainFragment extends Fragment {
private CallbackManager mCallBackManager;
private TextView mTextDetails;
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome" + profile.getName());
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public MainFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallBackManager=CallbackManager.Factory.create();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends", "user_photos");
loginButton.setFragment(this);
loginButton.registerCallback(mCallBackManager, mCallBack);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
mCallBackManager.onActivityResult(requestCode, resultCode, data);
}
}
This is my MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stan.facebooktestexactexample" >
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:name=".MyApplication"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id"/>
<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="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
</application>
</manifest>
And here is my MainFragment layout file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<TextView
android:text="#+id/text_details"
android:layout_width="match_parent"
android:gravity="center"
android:layout_above="#+id/login_button"
android:layout_height="wrap_content" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
and here is the logcat
--------- beginning of crash
07-25 05:12:55.211 2473-2473/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.stan.facebooktestexactexample, PID: 2473
java.lang.RuntimeException: Unable to instantiate application com.example.stan.facebooktestexactexample.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.stan.facebooktestexactexample.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.stan.facebooktestexactexample-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:563)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4529)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.stan.facebooktestexactexample.MyApplication" on path: DexPathList[[zip file "/data/app/com.example.stan.facebooktestexactexample-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newApplication(Instrumentation.java:980)
at android.app.LoadedApk.makeApplication(LoadedApk.java:558)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4529)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.ClassNotFoundException: com.example.stan.facebooktestexactexample.MyApplication
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
Please Help!!
if you set android:name=".MyApplication" on manifest you have to create MyApplication class.If you dont want that remove it or create class as
public class MyAppilcation extends Application{
...
}
it is used to declare global variables.
see this link How to declare global variables in Android?
I'm doing an app for Android. I have a main activity (not the default MainActivity.java, other activity named HotelPresentacion.java that has 3 buttons for insert/check registers or exit the app).
If I touch the Registrar button, supposedly I can register, but the app stops unexpectedly. If I touch the Registros button I can visualize the registers of my app, but when I touch one register (a short touch or long short) for only visualize or edit the app again stops unexpectedly.
I modified my androidmanifest.xml to set HotelPresentacion.java as my default starting activity.
This is the code of the HotelPresentacion.java
package com.example.lab007;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class HotelPresentacion extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel_presentacion);
}
public void onReservar(View v){
Intent i=new Intent(HotelPresentacion.this, ReservacionFormulario.class);
startActivity(i);
}
public void onVer(View v){
Intent i=new Intent(HotelPresentacion.this, MainActivity.class);
startActivity(i);
}
public void onSalir(View v){
finish();
}
}
My androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lab007"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HotelPresentacion"
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.lab007.ReservacionFormulario"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.lab007.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
LOGCAT
10-25 11:16:38.880: E/AndroidRuntime(4247): FATAL EXCEPTION: main
10-25 11:16:38.880: E/AndroidRuntime(4247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lab007/com.example.lab007.ReservacionFormulario}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.os.Looper.loop(Looper.java:130)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-25 11:16:38.880: E/AndroidRuntime(4247): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:16:38.880: E/AndroidRuntime(4247): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-25 11:16:38.880: E/AndroidRuntime(4247): at dalvik.system.NativeStart.main(Native Method)
10-25 11:16:38.880: E/AndroidRuntime(4247): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.Activity.setContentView(Activity.java:1657)
10-25 11:16:38.880: E/AndroidRuntime(4247): at com.example.lab007.ReservacionFormulario.onCreate(ReservacionFormulario.java:39)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-25 11:16:38.880: E/AndroidRuntime(4247): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-25 11:16:38.880: E/AndroidRuntime(4247): ... 11 more
The class ReservacionFormulario.java
package com.example.lab007;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class ReservacionFormulario extends ListActivity{
private ComplejoDBAdapter dbAdapterComplejo;
private ComplejoSpinnerAdapter complejoSpinnerAdapter;
private int modo ;
private long id ;
private Reservacion reserva = new Reservacion(this);
private EditText nombre;
private EditText apellidos;
//private DatePicker fechaInicio;
//private DatePicker fechaFin;
private Spinner complejo ;
private Button boton_guardar;
private Button boton_cancelar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservacion_formulario);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if (extra == null) return;
nombre = (EditText) findViewById(R.id.nombre);
apellidos = (EditText) findViewById(R.id.apellidos);
//fechaInicio = (DatePicker) findViewById(R.id.);
//fechaInicio = (DatePicker) findViewById(R.id.);
complejo = (Spinner) findViewById(R.id.complejo);
boton_guardar = (Button) findViewById(R.id.boton_guardar);
boton_cancelar = (Button) findViewById(R.id.boton_cancelar);
complejoSpinnerAdapter = new ComplejoSpinnerAdapter(this, Complejo.getAll(this, null));
complejo.setAdapter(complejoSpinnerAdapter);
if (extra.containsKey(ReservacionDBAdapter.C_COL_ID)){
id = extra.getLong(ReservacionDBAdapter.C_COL_ID);
consultar(id);
}
establecerModo(extra.getInt(ReservacionActivity.C_MODO));
boton_guardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
guardar();
}
});
boton_cancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
cancelar();
}
});
}
private void establecerModo(int m)
{
this.modo = m ;
if (modo == ReservacionActivity.C_VISUALIZAR){
this.setTitle(nombre.getText().toString());
this.setEdicion(false);
}
else if (modo == ReservacionActivity.C_CREAR){
this.setTitle("Nuevo reservacion");
this.setEdicion(true);
}
else if (modo == ReservacionActivity.C_EDITAR){
this.setTitle("Editar reservacion");
this.setEdicion(true);
}
}
private void consultar(long id){
reserva = Reservacion.find(this, id);
nombre.setText(reserva.getNombre());
apellidos.setText(reserva.getApellidos());
complejo.setSelection(complejoSpinnerAdapter.getPositionById(reserva.getComplejoId()));
}
private void setEdicion(boolean opcion){
nombre.setEnabled(opcion);
apellidos.setEnabled(opcion);
complejo.setEnabled(opcion);
// Controlamos visibilidad de botonera
LinearLayout v = (LinearLayout) findViewById(R.id.botonera);
if (opcion)
v.setVisibility(View.VISIBLE);
else
v.setVisibility(View.GONE);
}
private void guardar(){
/*verificar si funciona*/
try{
if(nombre.length()<=0){
reserva.setNombre(nombre.getText().toString());
}
}catch(Exception e){
}
reserva.setApellidos(apellidos.getText().toString());
reserva.setComplejoId(complejo.getSelectedItemId());
reserva.save();
if (modo == ReservacionActivity.C_CREAR){
Toast.makeText(ReservacionFormulario.this, "Creado", Toast.LENGTH_SHORT).show();
}
else if (modo == ReservacionActivity.C_EDITAR){
Toast.makeText(ReservacionFormulario.this, "Modificado", Toast.LENGTH_SHORT).show();
}
setResult(RESULT_OK);
finish();
}
private void cancelar(){
setResult(RESULT_CANCELED, null);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
if (modo == ReservacionActivity.C_VISUALIZAR)
getMenuInflater().inflate(R.menu.reservacion_formulario_ver, menu);
else
getMenuInflater().inflate(R.menu.reservacion_formulario_editar, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()){
case R.id.menu_eliminar:
borrar(id);
return true;
case R.id.menu_cancelar:
cancelar();
return true;
case R.id.menu_guardar:
guardar();
return true;
case R.id.menu_editar:
establecerModo(ReservacionActivity.C_EDITAR);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void borrar(final long id){
AlertDialog.Builder dialogEliminar = new AlertDialog.Builder(this);
dialogEliminar.setIcon(android.R.drawable.ic_dialog_alert);
dialogEliminar.setTitle("Eliminar");
dialogEliminar.setMessage("¿Desea eliminar?");
dialogEliminar.setCancelable(false);
dialogEliminar.setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int boton) {
reserva.delete();
Toast.makeText(ReservacionFormulario.this, "Eliminado", Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
});
dialogEliminar.setNegativeButton(android.R.string.no, null);
dialogEliminar.show();
}
}
The activity_reservacion_formulario.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<!-- Nombre -->
<TextView
android:id="#+id/label_nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nombre" />
<EditText
android:id="#+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/label_nombre"
android:maxLength="40"
android:ems="10" />
<!-- Apellidos -->
<TextView
android:id="#+id/label_apellidos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nombre"
android:maxLength="60"
android:text="Apellidos" />
<EditText
android:id="#+id/apellidos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/label_apellidos"
android:ems="10" />
<!-- Fecha de Inicio -->
<!-- <TextView
android:id="#+id/label_fechainicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/apellidos"
android:text="Fecha de Inicio" />
<DatePicker
android:id="#+id/datePickerInicio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_fechainicio" /> -->
<!-- Fecha de Fin -->
<!-- <TextView
android:id="#+id/label_fechafin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/datePickerInicio"
android:text="Fecha de Fin" />
<DatePicker
android:id="#+id/datePickerFin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_fechafin" /> -->
<!-- Spinner -->
<TextView
android:id="#+id/label_complejo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/datePickerFin"
android:text="Complejo" />
<Spinner
android:id="#+id/complejo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label_complejo" />
<TextView
android:id="#+id/label_ciudad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Complejo"
android:layout_below="#+id/complejo" />
<LinearLayout
android:id="#+id/botonera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:layout_below="#+id/datePickerFin"
android:orientation="horizontal" >
<Button
android:id="#+id/boton_cancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancelar" />
<Button
android:id="#+id/boton_guardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guardar" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
This is the entire project:
Project
I'll appreciante any help for solve my problem. Thanks
Replace
public class ReservacionFormulario extends ListActivity{
with
public class ReservacionFormulario extends Activity{
As you have extended the ListActivity so Android is searching for the a ListView with Id list, but In your XML there is no any such type of ListView.
From the crash log i guess its.
Change ListActivity to Activity
I'm pretty new to Android development so I'm sure this is going to turn out to be something stupid that I've missed, but I have two activities: StartScreen and Map. StartScreen is just simple text and a button, which is supposed to then take you to the Map activity.
I've already tried reducing the Map activity down to almost nothing, and I'm sure it's nothing in there that's causing the error, but I just can't figure out what is.
This is the logcat output for the error, it happens when I press the button linking the two activities:
04-28 20:40:24.097: E/AndroidRuntime(13458): FATAL EXCEPTION: main
04-28 20:40:24.097: E/AndroidRuntime(13458): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rr.freespace/com.rr.freespace.Map}: java.lang.NullPointerException
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.os.Looper.loop(Looper.java:154)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread.main(ActivityThread.java:4945)
04-28 20:40:24.097: E/AndroidRuntime(13458): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 20:40:24.097: E/AndroidRuntime(13458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-28 20:40:24.097: E/AndroidRuntime(13458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-28 20:40:24.097: E/AndroidRuntime(13458): at dalvik.system.NativeStart.main(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458): Caused by: java.lang.NullPointerException
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.content.ComponentName.<init>(ComponentName.java:75)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.content.Intent.<init>(Intent.java:3475)
04-28 20:40:24.097: E/AndroidRuntime(13458): at com.rr.freespace.Map.<init>(Map.java:12)
04-28 20:40:24.097: E/AndroidRuntime(13458): at java.lang.Class.newInstanceImpl(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458): at java.lang.Class.newInstance(Class.java:1319)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
04-28 20:40:24.097: E/AndroidRuntime(13458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
Here's my StartScreen.java:
package com.rr.freespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class StartScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
}
public void doIt(View view) { // Do something in response to button }
Intent intent = new Intent(this, Map.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start_screen, menu);
return true;
}
}
The activity_start_screen.xml layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".StartScreen" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:text="#string/hometext2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:text="#string/doit"
android:onClick="doIt" />
</RelativeLayout>
And finally, the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rr.freespace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.rr.freespace.permission.MAPS_RECEIVE"/>
<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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBaoeVjdRY312vJ5KMLXxmzpKHhPiYqASo"/>
<activity
android:name="com.rr.freespace.StartScreen"
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.rr.freespace.Map"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
I hope someone can figure out what's going on here, because I can't! Cheers!
EDIT: here's the map.java code, there may be more errors in here that I haven't got to yet, because I hacked most of this together yesterday before I had access to an android device:
package com.rr.freespace;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class Map extends Activity {
final Context context = this;
Intent goback = new Intent(this, StartScreen.class);
protected LocationManager locationManager;
protected LocationListener listener;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
AlertDialog.Builder gpssettings = new AlertDialog.Builder(context);
gpssettings.setMessage(R.string.gpstext)
.setTitle(R.string.gpstitle);
gpssettings.setPositiveButton(R.string.gpsok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
enableLocationSettings();
}
});
gpssettings.setNegativeButton(R.string.gpscancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
startActivity(goback);
}
});
}
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(false)
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
//Get GPS data
final LocationListener listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// A new location update is received. Do something useful with it. Update the UI with
// the location update.
final LatLng position = new LatLng(location.getLatitude(),location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, // 10-second interval.
5, // 5 meters.
listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
private void enableLocationSettings() {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
protected void onStop() {
super.onStop();
locationManager.removeUpdates(listener);
}
}
Intent goback = new Intent(this, StartScreen.class);
that causes an issue.
You can't use "this" here, best to create intents just before using them.
But if You would like to have it as soon as possible do it in onCreate.