Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

sqlite - Log statement giving error on printing the value of Intent in android

This is getting me error and I don't know how to solve this. I want to use the values passed from other intent.

package com.example.user.shopkeeper.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.user.shopkeeper.R;

public class UserActivity extends AppCompatActivity  {
    SharedPreferences sp;
    private String name, contact, user_id, email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

//        initViews();
        Bundle extras = getIntent().getExtras();
        if(extras == null){
            email = null;
            name = null;
            contact = null;
            user_id = null;
        }
        else{
            email = extras.getString("Email");
            name = extras.getString("Name");
            user_id = extras.getString("User_Id");
            contact = extras.getString("Contact");
        }
        Log.d("User : ",user_id);
        FloatingActionButton add_user = findViewById(R.id.floatingActionButton7);
        add_user.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(UserActivity.this, AddNewMember.class);
                    i.putExtra("Name",name);
                    i.putExtra("User_Id",user_id);
                    i.putExtra("Contact",contact);
                    startActivity(i);
                }
        });



    }

//    public void initViews(){
//        // Values from loginActivity area which is name,email,id,contact
//
//        Intent intent = getIntent();
//        email = intent.getStringExtra("Email");
//        name = intent.getStringExtra("Name");
//        user_id = intent.getStringExtra("User_Id");
//        contact = intent.getStringExtra("Contact");
//
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.logout:

                sp = getSharedPreferences(LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                editor.apply();

                Intent in = new Intent(UserActivity.this,LoginActivity.class);
                startActivity(in);
                finish();
                return true;
        }
        return false;
    }

I have updated both the files,, the one from where I am sending the values and the other from where I am trying to receive

One is LoginActivity File and the other is UserActivity file.

package com.example.user.shopkeeper.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.View;

import com.example.user.shopkeeper.R;
import com.example.user.shopkeeper.helper.InputValidation;
import com.example.user.shopkeeper.sql.DatabaseHelper;

import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    public final AppCompatActivity activity = LoginActivity.this;
    private ConstraintLayout constraintLayout;
    private TextInputLayout eT_email;
    private TextInputLayout eT_password;
    private TextInputEditText email;
    private TextInputEditText password;
    private AppCompatButton login;
    private AppCompatTextView sign_up;

    private InputValidation inputValidation;
    private DatabaseHelper databaseHelper;

    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Phone = "phoneKey";
    public static final String Email = "emailKey";
    SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        getSupportActionBar().hide();
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        initViews();
        initListeners();
        initObjects();

        if(sharedpreferences.getBoolean("logged",false)){
            Intent accountsIntent = new Intent(activity,UserActivity.class);
            accountsIntent.putExtra("EMAIL",email.getText().toString().trim());
            startActivity(accountsIntent);
            finish();
        }

    }

    private void initViews(){
        constraintLayout = findViewById(R.id.constraint_layout);
        eT_email = findViewById(R.id.eT_email);
        eT_password = findViewById(R.id.eT_Password);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        sign_up = findViewById(R.id.sign_up);
    }

    private void initListeners(){
        login.setOnClickListener(this);
        sign_up.setOnClickListener(this);
    }

    private void initObjects(){
        databaseHelper = new DatabaseHelper(activity);
        inputValidation = new InputValidation(activity);
    }

    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.login:
                verifyFromSQLite();
                break;
            case R.id.sign_up:
                Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(register);
                break;
        }
    }

    private void verifyFromSQLite(){
        if(!inputValidation.isInputTextFilled(email,eT_email,getString(R.string.error_message_email))){
            return;
        }
        if(!inputValidation.eT_email(email,eT_email,getString(R.string.error_message_email))){
            return;
        }
        if(!inputValidation.isInputTextFilled(password,eT_password,getString(R.string.error_message_password))){
            return;
        }



        if(databaseHelper.checkUser(email.getText().toString().trim(),password.getText().toString().trim())){


            DatabaseHelper db = new DatabaseHelper(LoginActivity.this);
            List<String> contactNumber = db.getContactNumber(email.getText().toString().trim());
            String contact_db = contactNumber.get(0);
            String name_db = contactNumber.get(1);
            String user_id_db = contactNumber.get(2);
            Log.d("Contact ", contact_db);
            Log.d("name_db ", name_db);
            Log.d("User Id  ", user_id_db);

            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString(Phone,contact_db);
            editor.putString(Email,email.getText().toString().trim());
            editor.putBoolean("logged",true).apply();
            editor.apply();

            Intent accountsIntent = new Intent(activity,UserActivity.class);

            Bundle extras = new Bundle();

            extras.putString("Email", email.getText().toString().trim());
            extras.putString("Name", name_db);
            extras.putString("User_Id", user_id_db);
            extras.putString("Contact", contact_db);

            accountsIntent.putExtras(extras);

            emptyInputEditText();
            startActivity(accountsIntent);
            finish();
        }
        else{
            Snackbar.make(findViewById(android.R.id.content),getString(R.string.error_valid_email_password),Snackbar.LENGTH_LONG).show();
        }
    }

    private void emptyInputEditText(){
        email.setText(null);
        password.setText(null);
    }
}

The bundle is also giving the error in this case.

11-28 11:57:30.477 16854-16854/com.example.user.shopkeeper E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.shopkeeper, PID: 16854
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.shopkeeper/com.example.user.shopkeeper.activities.UserActivity}: java.lang.NullPointerException: println needs a message
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2792)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:172)
        at android.app.ActivityThread.main(ActivityThread.java:6590)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.d(Log.java:145)
        at com.example.user.shopkeeper.activities.UserActivity.onCreate(UserActivity.java:40)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Activity.performCreate(Activity.java:7014)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)?
        at android.app.ActivityThread.-wrap11(Unknown Source:0)?
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)?
        at android.os.Handler.dispatchMessage(Handler.java:106)?
        at android.os.Looper.loop(Looper.java:172)?
        at android.app.ActivityThread.main(ActivityThread.java:6590)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)?

The error is this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

build the Intent alike this:

Intent intent = new Intent(activity, UserActivity.class);
Bundle extras = new Bundle();

extras.putString("Email", email.getText().toString().trim());
extras.putString("Name", name_db);
extras.putString("User_Id", user_id_db);
extras.putString("Contact", contact_db);

intent.putExtras(extras);
startActivity(intent);

while the List<String> contactNumber = db.getContactNumber() already seems unfortunate.

it might be rather elegant to return some class Contact (to be defined) instead of a List<String>

... so that one does not have to use index-access for getting the details.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(this.getIntent() != null) {
        String email = this.getIntent().getStringExtra("Email");
        Log.d("UserActivity", email);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...