致命异常:主java.lang.RuntimeException:无法启动活动ComponentInfo()

时间:2016-03-26 06:10:02

标签: android android-fragments android-studio android-viewpager

我遇到了这个问题并且我已经按照this回答但无法解决我的问题。我认为它是一样的。

我想在Activity页面上创建两个不同的标签,但结果却无效。

我不确定代码有什么问题。

MainActivity.java

package pasirgudangcouncilstate.infocenter;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
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;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    NavigationView navigationView = null;
    Toolbar toolbar = null;

    ViewPager pager;
    PagerTabStrip tab_strp;

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

        ActivityFragment mapager = new ActivityFragment(getSupportFragmentManager());

        pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(mapager);
        tab_strp=(PagerTabStrip)findViewById(R.id.tab_strip);
        tab_strp.setTextColor(Color.WHITE);

        //        Set Fragment Initially
        UtamaFragment fragment = new UtamaFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        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) 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_home) {

            //        Set Fragment Initially
            UtamaFragment fragment = new UtamaFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
            // Handle the camera action
        } else if (id == R.id.nav_background) {

            //        Set Fragment Initially
            LatarBelakangFragment fragment = new LatarBelakangFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

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

            //        Set Fragment Initially
            PerkhidmatanFragment fragment = new PerkhidmatanFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

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

            ActivityFragment mapager = new ActivityFragment(getSupportFragmentManager());
            pager = (ViewPager)findViewById(R.id.pager);

            pager.setAdapter(mapager);
            tab_strp=(PagerTabStrip)findViewById(R.id.tab_strip);
            tab_strp.setTextColor(Color.WHITE);

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

        }

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

activity_main.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</android.support.v4.widget.DrawerLayout>

logcat的

    03-26 05:57:16.932 2158-2158/pasirgudangcouncilstate.infocenter I/art: Not late-enabling -Xcheck:jni (already on)
    03-26 05:57:17.095 2158-2158/pasirgudangcouncilstate.infocenter W/System: ClassLoader referenced unknown path: /data/app/pasirgudangcouncilstate.infocenter-2/lib/x86
    03-26 05:57:17.598 2158-2158/pasirgudangcouncilstate.infocenter D/AndroidRuntime: Shutting down VM
    03-26 05:57:17.598 2158-2158/pasirgudangcouncilstate.infocenter E/AndroidRuntime: FATAL EXCEPTION: main
Process: pasirgudangcouncilstate.infocenter, PID: 2158
java.lang.RuntimeException: Unable to start activity ComponentInfo{pasirgudangcouncilstate.infocenter/pasirgudangcouncilstate.infocenter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.PagerTabStrip.setTextColor(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.PagerTabStrip.setTextColor(int)' on a null object reference
at pasirgudangcouncilstate.infocenter.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    03-26 05:57:21.767 2158-2160/pasirgudangcouncilstate.infocenter W/art: Suspending all threads took: 7.989ms
    03-26 05:57:24.039 2158-2160/pasirgudangcouncilstate.infocenter W/art: Suspending all threads took: 35.938ms
    03-26 05:57:28.562 2158-2158/? I/Process: Sending signal. PID: 2158 SIG: 9

2 个答案:

答案 0 :(得分:1)

删除您的代码并直接添加。

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         > 
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/tab_strp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="5dp"
        android:paddingBottom="5dp" />    

</android.support.v4.view.ViewPager>

并在您的活动代码中删除此行。

tab_strp.setTextColor(Color.WHITE);

答案 1 :(得分:1)

首先,我建议使用TabLayout代替PagerTabStrip

其次,我没有在您的xml中看到PagerTabStrip,这就是您获取NullPointerException的原因。删除它,并尝试在没有它的情况下使ViewPager工作,稍后只需在this教程后添加它。