无法启动活动NullPointerException TextView.setText

时间:2018-11-02 22:44:18

标签: java android nullpointerexception

我的Android应用程序有问题。我尝试了StackOverflow的另一种解决方案,但没有结果。现在,我真的不知道我在哪里出错。我知道,变量信息为空,但我不知道为什么,因为它是在activity_dashboard.xml中定义的,而AndroidStudio却悄悄地告诉我该ID。

错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: cz.revelio.mhdapp, PID: 16181
java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.revelio.mhdapp/cz.revelio.mhdapp.DashboardActivity}: 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:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at cz.revelio.mhdapp.DashboardActivity.onCreate(DashboardActivity.java:27)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

DashboardActivity.java

package cz.revelio.mhdapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

public class DashboardActivity extends BaseActivity{
    RecyclerView recyclerView;
    DatabaseHelper mDatabaseHelper;
    TextView info;
    Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDatabaseHelper = new DatabaseHelper(this);
    int vsechyZaznamy = mDatabaseHelper.getCountAllLogs();
    int unikatniZaznamy = mDatabaseHelper.getCountUniqueLogs();
    info = (TextView) findViewById(R.id.info_count_in_dash);
    info.setText(vsechyZaznamy+" záznamů, z toho "+unikatniZaznamy+" unikátních.");
    recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
    getData();
}
public void getData(){
    List<String> dataZaznamy = mDatabaseHelper.getAllLogs();
    String[] zaznamy = new String[dataZaznamy.size()];
    zaznamy = dataZaznamy.toArray(zaznamy);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(new Adapter(this,zaznamy));
}

}

BaseActivity.java

    package cz.revelio.mhdapp;
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.Toast;

public class BaseActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);
    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.addDrawerListener(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 onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.base, menu);
    return true;
}

@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_send) {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    } else if (id == R.id.nav_gallery) {
        Intent intent = new Intent(this, DashboardActivity.class);
        startActivity(intent);
    } else if (id == R.id.nav_slideshow) {
        Toast.makeText(this, "Zatím není k dispozici", Toast.LENGTH_SHORT);
    } else if (id == R.id.nav_manage) {
        Toast.makeText(this, "Zatím není k dispozici", Toast.LENGTH_SHORT);
    } else if (id == R.id.nav_share) {
        Toast.makeText(this, "Zatím není k dispozici", Toast.LENGTH_SHORT);
    } else if (id == R.id.nav_send2) {
        Toast.makeText(this, "Zatím není k dispozici", Toast.LENGTH_SHORT);
    }

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

activity_dashboard.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cz.revelio.mhdapp.DashboardActivity">


<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    >

    <TextView
        android:id="@+id/info_count_in_dash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="5"
        tools:listitem="@layout/costume_row" />

</FrameLayout>

1 个答案:

答案 0 :(得分:1)

我认为它不会那样工作。您没有在“仪表板”活动中设置内容视图。这就是为什么它找不到textview并且结果为null的原因。活动开始时,它将调用DashboardActivity的onCreate而不是BaseActivity的onCreate。

如果您认为既然已经在基类的onCreate中创建了内容,那么它将为子类自动创建内容,那么您就错了。