【问题标题】:I try to set text for textview from fetching database but i am unable to do that我尝试从获取数据库中为 textview 设置文本,但我无法做到这一点
【发布时间】:2016-09-09 05:36:37
【问题描述】:

这是我完成登录代码的类,登录后它会获取用户的姓名和电子邮件到鞋子。 获取过程运行良好,但是当我尝试设置不同布局 xml 文件的文本时,它给了我空指针异常。

这是 NavigationMenu 类这里是我的登录功能

   FragmentTransaction fragmentTransaction;
    NavigationView navigationView;
    RoundImage roundedImage;
    ImageView profileImage;
    TextView uname,mail;

    private SQLiteHandler db;
    private SessionManager session;

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

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


        db = new SQLiteHandler(getApplicationContext());

        HashMap<String,String> user = db.getUserDetails();

        String name = user.get("name");
        String email = user.get("email");

        Log.d("Soulsystem", name);
        Log.d("Soulsystem", email);

        uname = (TextView)findViewById(R.id.alluser);
        mail = (TextView) findViewById(R.id.allemail);

        try {
            uname.setText(name);
            mail.setText(email);
        }catch (NullPointerException NPE)
        {
            Log.d("Soul system","Here is the error we are setting null values");
        }
       /* profileImage = (ImageView)view.findViewById(R.id.navigation_view_Image);
        profileImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("NavigationMenu", "In profile Image view");
                Intent i = new Intent(NavigationMenu.this, ClientProfile.class);
            }
        });*/

        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);

        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container,new HomeFragment());
        fragmentTransaction.commit();

        // session manager
        session = new SessionManager(getApplicationContext());

        if (!session.isLoggedIn()) {
            logoutUser();
        }

        navigationView = (NavigationView)findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                switch (item.getItemId()){

                    case R.id.profile:

                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new ClientProfile());
                        fragmentTransaction.commit();
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;

                    case R.id.logout:
                        logoutUser();
                       /* item.setChecked(true);
                        drawerLayout.closeDrawers();*/
                        break;
                }
                return true;
            }
        });


    }
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        actionBarDrawerToggle.syncState();
        super.onPostCreate(savedInstanceState);
    }

    private void logoutUser() {
        session.setLogin(false);

        db.deleteUsers();
        Intent intent = new Intent(NavigationMenu.this, MainActivity.class);
        startActivity(intent);
        finish();
    }


}

这是一个导航菜单xml文件,我尝试设置文本及其工作文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context="com.example.user.soulsystem.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_container">
            <TextView
                android:id="@+id/alluser"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:gravity="center_horizontal"
                android:id="@+id/allemail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>

    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/drawe_menu">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

这是我想在其中显示文本的 xml 我该怎么做 我尝试使用 Layout Interline,但是当我尝试设置文本时,它在 setText() 上给了我空指针异常

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@color/bg_register">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/navigation_view_Image"
        android:background="@drawable/image"
        android:src="@drawable/profile_image"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"/>

    <TextView
        android:layout_below="@id/navigation_view_Image"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:id="@+id/username"
        android:textColor="@color/Black"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/navigation_view_Image"
        android:layout_alignStart="@+id/navigation_view_Image" />
        </LinearLayout>
    <TextView
        android:layout_below="@id/username"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:id="@+id/email"
        android:textColor="@color/Black"
        android:layout_alignLeft="@+id/navigation_view_Image"
        android:layout_alignStart="@+id/navigation_view_Image" />
</LinearLayout>

【问题讨论】:

  • "它给了我空指针异常" -- 好的,然后显示 logcat。报错怎么办,不清楚问题出在哪里?
  • 引起:java.lang.NullPointerException:尝试在 com.example.user 的空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)' .soulsystem.NavigationMenu.onCreate(Navigati‌​onMenu.java:57)
  • 当我从第二个 xml 文件调用用户名文本视图时发生错误
  • 您不能使用 findViewById 从 Activity 类中查找 Fragment 布局值。为什么不能从 Fragment 中查询数据库并在那里更新 TextView?
  • 我也试过了。我创建了类并使用片段进行了扩展,并在其中调用了第二个 xml,并尝试在 onCreate 视图中设置文本,但也没有工作

标签: android android-fragments arraylist android-database


【解决方案1】:

将此行替换为

db = new SQLiteHandler(getApplicationContext());

db = new SQLiteHandler(this);

它可以正常工作,因为 getApplicationContext() 返回整个应用程序的上下文,但 'this' 返回正在运行的 Activity 的上下文。

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2021-01-16
    • 2012-03-07
    • 1970-01-01
    • 2020-11-14
    • 2019-06-05
    相关资源
    最近更新 更多