Textview findviewbyid Fragment Mainactivity

Asked

Viewed 180 times

-1

I’m trying to give a setText() in Textview q is in Fragment , but returns me this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: kcollector.com.br.kvendedor, PID: 24201
java.lang.RuntimeException: Unable to start activity ComponentInfo{kcollector.com.br.kvendedor/kcollector.com.br.kvendedor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6923)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at kcollector.com.br.kvendedor.MainActivity.verificarUsuario(MainActivity.java:83)
    at kcollector.com.br.kvendedor.MainActivity.onCreate(MainActivity.java:69)
    at android.app.Activity.performCreate(Activity.java:7148)
    at android.app.Activity.performCreate(Activity.java:7139)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 

The Fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="@dimen/nav_header_height"
          android:background="@drawable/side_nav_bar"
          android:gravity="bottom"
          android:orientation="vertical"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:theme="@style/ThemeOverlay.AppCompat.Dark">

<TextView
        android:id="@+id/nome_pessoa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nav_header_title"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

<TextView
        android:id="@+id/dc_email_pessoa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nav_header_subtitle"/>

o Mainactivity:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
Context context = this;
public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
Date hoje;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    navigationView.setNavigationItemSelectedListener(this);
    //carregar o ImportFragmente BLANK
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.conteudo, new ImportFragment()).commit();

    //fragmentManager.beginTransaction().replace(R.id.conteudo, new PedidoFragment()).commit();
    verificarUsuario();

}

protected void verificarUsuario(){
    Pessoa pessoa = Pessoa.first(Pessoa.class);

    if(pessoa.getDc_token().isEmpty()){
        Intent intent = new Intent(context, Login.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }else{

        TextView nome = findViewById(R.id.nome_pessoa);
        nome.setText(pessoa.getNo_nome_completo());

        TextView email = findViewById(R.id.dc_email_pessoa);
        email.setText(pessoa.getDc_email());
    }

}

1 answer

0


You are trying to access the textview of Fragment within Activity. Logico these Textview does not exist in Activity so you are taking the Nullpointerexception

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

That one verificarUsuario() need to be inside your Fragment to access the Textview from there.

  • Sorry Welbert, but you could tell me cm I do it?

  • The method to check Language() needs to be inside the Fragment 'Importfragment' in the class Importfragment.Java

  • Thanks master, solved my problem. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.