-2

I have created an application with multiple activities, all were working fine, until i added an email activity, now the app crashes even without going to main activity. email activity send a recommendation of this app to your friends.

but i don't know what changes need to be made in the app manifest.

package com.example.androidhive;
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;

public class mailactivity extends Activity{

Button mailsend;
Button mail;
EditText mailname;
EditText mailaddr;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mail);


    mailsend = (Button) findViewById(R.id.mailsend);
    mailname = (EditText) findViewById(R.id.mailname);
    mailaddr = (EditText) findViewById(R.id.mailaddr);


    mailsend.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

          String to = mailaddr.getText().toString();
          String subject = mailname.getText().toString() + " refered an app";
          String message = "Hey check out this cool app made my CIET students";

          Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, message);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
  }
}

main activity:

package com.example.androidhive;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;

public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
Button mail;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    // Importing all assets like buttons, text fields
    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            Log.d("Button", "Login");
            JSONObject json = userFunction.loginUser(email, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    }else{
                        // Error in login
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });

    // link to mail screen
    mail.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent j = new Intent(getApplicationContext(),
                    mailactivity.class);
            startActivity(j);
            finish();
        }
    });

  }
}

manifest file:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:debuggable="true">
    <activity
        android:label="@string/app_name"
        android:name=".DashboardActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--  Login Activity -->
    <activity
        android:label="Login Account" 
        android:name=".LoginActivity"></activity>

    <!--  Register Activity -->
    <activity
        android:label="Register New Account" 
        android:name=".RegisterActivity"></activity>

    <activity
        android:label="Send email" 
        android:name=".mailactivity"></activity>

</application>

<!-- Allow to connect with internet -->
<uses-permission android:name="android.permission.INTERNET" />

Logcat output:

07-31 10:23:20.525: E/AndroidRuntime(1275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.LoginActivity}: java.lang.NullPointerException
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.os.Looper.loop(Looper.java:137)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at dalvik.system.NativeStart.main(Native Method)
07-31 10:23:20.525: E/AndroidRuntime(1275): Caused by: java.lang.NullPointerException
07-31 10:23:20.525: E/AndroidRuntime(1275):     at com.example.androidhive.LoginActivity.onCreate(LoginActivity.java:111)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.Activity.performCreate(Activity.java:5104)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-31 10:23:20.525: E/AndroidRuntime(1275):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
supersayan
  • 13
  • 5
  • Have you added permissions in manifest? Show logcat – MysticMagicϡ Jul 31 '14 at 10:17
  • i have added internet permission, and another but i dont know what goes as android:label, – supersayan Jul 31 '14 at 10:19
  • 1
    we MUST make posting logcat for all android-related questions MANDATORY! – injecteer Jul 31 '14 at 10:20
  • The title is almost as useless as "Code does not work after changing it". You want to know "what changes need to be made in the app manifest" but we have no idea how your manifest looks. You are talking about a crash but you neither posted logcat output nor a stack trace. – Tobias Jul 31 '14 at 10:20
  • [2014-07-31 15:36:01 - AndroidLoginAndRegistrationWithMySQLPHPSQLite] Starting activity com.example.androidhive.DashboardActivity on device emulator-5554 [2014-07-31 15:36:03 - AndroidLoginAndRegistrationWithMySQLPHPSQLite] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.androidhive/.DashboardActivity } – supersayan Jul 31 '14 at 10:21
  • @supersayan I added the log entries you posted to your initial post, but they are useless as they don't contain information about the crash. – Tobias Jul 31 '14 at 10:23
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jul 31 '14 at 10:23
  • any way to post the log as text file? rather than copy paste? – supersayan Jul 31 '14 at 10:27
  • Run this app in your real device – Piyush Jul 31 '14 at 10:29
  • The log clearly says that the error is in loginactivity and not in mailactivity that you have posted – kgandroid Jul 31 '14 at 10:30
  • i know i am supposed to add some intent filters but dont know what exactly! you guys could rather be obvious about the answers – supersayan Jul 31 '14 at 10:30
  • show ur manifest file – Piyush Jul 31 '14 at 10:31
  • What's line LoginActivity.java:111? – MysticMagicϡ Jul 31 '14 at 10:36
  • there is problem with your `LoginActivity.java`... not with mailactivity.. – Pragnesh Ghoda シ Jul 31 '14 at 10:48
  • shall i post loginactivity? as an edit? – supersayan Jul 31 '14 at 10:50

2 Answers2

0

Try nesting your email intents between try catch blocks, ask if your still unclear

You forgot to import button asset Add mail= (button) findViewById(R.Id."where ur button located")

Akash
  • 220
  • 3
  • 12
0
      Intent email = new Intent(Intent.ACTION_SEND);
      email.setData(Uri.parse("mailto:"));
      email.setType("text/plain");
      email.putExtra(Intent.EXTRA_EMAIL,to);
      email.putExtra(Intent.EXTRA_SUBJECT, subject);
      email.putExtra(Intent.EXTRA_TEXT, message);

  try {
     startActivity(Intent.createChooser(email, "Choose an Email client :"));
     finish();
     Log.i("Finished sending email...", "");
  } catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(mailactivity .this, 
     "There is no email client installed.", Toast.LENGTH_SHORT).show();
  }

}

PLP
  • 714
  • 3
  • 13