错误:尝试在空对象引用上

时间:2019-10-28 04:26:09

标签: android android-fragments android-activity

我正在尝试将数据从Activity传递到Fragment。

这是我的通话活动。使用此活动,我想将数据传递到ProfileFragment 为此,我正在使用捆绑软件。 但我在ProfileFragment上收到错误,如上所示

NewAccount.java

package com.example.takeattendence;

import androidx.appcompat.app.AppCompatActivity;


import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Patterns;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.example.takeattendence.database.LoginContract.LoginEntry;
import com.example.takeattendence.database.LoginContractStudent;
import com.example.takeattendence.database.LoginDbHelper;
import com.example.takeattendence.database.LoginContractStudent.LoginEntryStudent;


public class NewAccount extends AppCompatActivity
{




    //EditText field to First name of user
    private EditText mFirstNameEditText;

    //EditText field to Last Name of user
    private EditText mLastNameEditText;

    //EditText field to Phone Number of user
    private EditText mPhoneNumberEditText;

    //EditText field to Email Id of user
    private EditText mEmailIdEditText;

    //EditText field to Password of User
    private EditText mPasswordEditText;

    //Spinner field for post
    private Spinner mPostSpinner;


    private String mPost = "Leturer";

    //Spinner field to gender of user
    private Spinner mGenderSpinner;

    private String mGender = "Other";

    //Button for Register
    private Button mRegisterButton;

    //Textview for Log in
    private TextView mLogInTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_account);




        mFirstNameEditText =(EditText)findViewById(R.id.editText_first_name);
        mLastNameEditText = (EditText)findViewById(R.id.editText_last_name);
        mPhoneNumberEditText =(EditText)findViewById(R.id.editText_phone_number);
        mEmailIdEditText = (EditText) findViewById(R.id.editText_email_id_sign_up);
        mPasswordEditText =(EditText) findViewById(R.id.editText_pass_sign_up);
        mPostSpinner = (Spinner)findViewById(R.id.spinner_post);
        mGenderSpinner=(Spinner) findViewById(R.id.spinner_gender);
        mRegisterButton = (Button) findViewById(R.id.button_register);
        mLogInTextView =  (TextView) findViewById(R.id.textview_log_in);

        setUpSpinnerPost();
        setUpSpinnerGender();

        mLogInTextView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(NewAccount.this,MainActivity.class);
                startActivity(i);
            }
        });
        final LoginDbHelper mLoginDbHelper = new LoginDbHelper(this);


        mRegisterButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //insert data
                insertUserData();

            }
        });


    }

    public void insertUserData()
    {

        Bundle bundle = new Bundle();
        bundle.putString("data", "Data");
        // Your fragment
        ProfileFragment obj = new ProfileFragment();
        obj.setArguments(bundle);

        Intent i = new Intent(NewAccount.this,WelcomeApp.class);
        startActivity(i);
    }


}

WelcomeApp.java

package com.example.takeattendence;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationView;

public class WelcomeApp extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
    private DrawerLayout drawerLayout;

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

        Toolbar toolbar = findViewById(R.id.nav_toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = findViewById(R.id.drawer_layout);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                R.string.navigation_drawer_open,R.string.navigation_drawer_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        if(savedInstanceState == null)
       {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ProfileFragment()).commit();

//            Intent i = new Intent(WelcomeApp.this, TestAcitivity.class);
//            startActivity(i);
            navigationView.setCheckedItem(R.id.nav_profile);
        }

//        finish();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
    {
        switch (menuItem.getItemId())
        {
            case R.id.nav_profile:

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ProfileFragment()).commit();
                break;

            case R.id.nav_update_profile:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new UpdateProfileFragment()).commit();
                break;

            case R.id.nav_see_database:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new StudenetInfoFragment()).commit();
                break;

            case R.id.nav_logout:
//                Intent in = new Intent(WelcomeApp.this,MainActivity.class);
//                startActivity(in);
//                finish();

//                SharedPreferences.Editor editor = getSharedPreferences("POS", MODE_PRIVATE).edit();
//                editor.putString("password", "");
//                editor.putString("email", "");
//                editor.putBoolean("isLoggedIn", false);
//                editor.apply();
//
//                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
//                intent.putExtra("finish", true);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);

//                finish();

                SharedPreferences.Editor editor = getSharedPreferences("name", MODE_PRIVATE).edit();
                editor.putString("password", "");
                editor.putString("email", "");
                editor.putBoolean("isLoggedIn", false);
                editor.apply();

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("finish", true);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

                finish();

                break;

            default:
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
    @Override
    public void onBackPressed()
    {
        moveTaskToBack(true);
    }

}

ProfileFragment.java

package com.example.takeattendence;

import android.content.Intent;

import android.graphics.Bitmap;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;


import static android.app.Activity.RESULT_OK;

public class ProfileFragment extends Fragment //implements LoaderManager.LoaderCallbacks<Cursor>
{


    public ProfileFragment()
    {

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_profile,container,false);

        TextView firstNameTextView = (TextView)view.findViewById(R.id.textview_profile_first_name);

        String strtext = this.getArguments().getString("data");
        firstNameTextView.setText(strtext);
        return view;


    }

我在Google上进行了搜索,但没有得到适当的解决方案。 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

在片段实例化中使用它:

ProfileFragment frag = new ProfileFragment();
Bundle b = new Bundle();
b.putString("data", "your data goes here");
frag.setArguments(b);

getSupportFragmentManager()。beginTransaction()。replace(R.id.fragment_container,frag)     .commit();