【问题标题】:setText on ID not working in android studioID上的setText在android studio中不起作用
【发布时间】:2017-01-13 12:30:36
【问题描述】:

我的 xml 中有一个 textview,我想更改意图中的文本。我之前在同一个课程中尝试过,但在另一个 xml 上尝试过,它在那里工作。但是在更改 contentView 后,它不再起作用了。我想设置 displayName。

package de.myapp.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class UserAreaActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("");
        //setContentView(R.layout.nav_header_user_area);
        //final TextView displayName = (TextView) findViewById(R.id.displayName);
        //Intent intent = getIntent();
        //String dpdisplayName = intent.getStringExtra("displayName");
        //displayName.setText(dpdisplayName);

        setContentView(R.layout.activity_user_area);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

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

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @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);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

XML:

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

【问题讨论】:

  • 为什么有 2 个布局?
  • 因为导航。在标准的android导航中,bar有一个自己的布局,另一个布局是主屏幕。
  • 你不能在一个活动中拥有两个 setContentView。
  • 在第一个布局中设置文本后,我想回到你理解的主要内容?
  • 啊,这就是重点。我能做什么?

标签: java android


【解决方案1】:

[更新] 像这样使用:

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

 Intent intentMain = getIntent();
 String dpdisplayName = intentMain.getStringExtra("displayName");

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

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
 drawer.setDrawerListener(toggle);
 toggle.syncState();

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
 View headerView = navigationView.getHeaderView(0);
 TextView navdisplayName = (TextView) headerView.findViewById(R.id.displayName);
 navdisplayName.setText(dpdisplayName);
}

【讨论】:

  • java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'
  • 因为 displayName 不在 activity_user_are 中,而是在 nav_header_user_area 中。这就是问题所在,应用程序崩溃了
  • 是的,我错过了将 contentView 更改为 nav_header_user_area
  • 但是在设置了 displayName 之后我需要回到 activity_user_area 因为剩下的代码在这里播放。
  • 回去的意思,是活动还是什么?
【解决方案2】:

首先删除

多次调用

  setContentView(R.layout.activity_user_area); // Remove from 2nd time 

最终视图

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Dont call setTitle(""); Here
    setContentView(R.layout.nav_header_user_area);
    final TextView displayName = (TextView) findViewById(R.id.displayName);
    Intent intent = getIntent();
    String dpdisplayName = intent.getStringExtra("displayName");
    displayName.setText(dpdisplayName);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle("");
  //code goes on here
  }

【讨论】:

    【解决方案3】:

    在第二次设置 setContentView() 时,您最终更改了附加的视图层次结构树。您对 textview 的更改将不再可见,因为它甚至没有附加到活动

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2014-04-09
      • 2021-08-02
      相关资源
      最近更新 更多