java.lang.RuntimeException:无法启动活动ComponentInfo {....}:java.lang.NullPointerException

时间:2014-05-23 05:35:45

标签: java android eclipse android-intent nullpointerexception

MainActivity中的

public class MainActivity extends AbstractActivity implements OnClickListener {

public static final int REQUEST_CODE_OUTLET = 0;

private ViewFlipper mainLayout;
private Button tabOutlet;
private Button tabSync;
private Button tabHistory;
private Button tabSettings;

private boolean isOutletScreen = true;

private OutletListView outletListView;
private SurveyResultListView surveyResultListView;
private HistoryListView historyListView;
private SettingsView settingsView;
private TabViewOutletSaleRep tabViewOutLetSaleRep;
private GPSTracker gps;

public MainActivity() {
    super(R.layout.activity_main, true);
}

@Override
protected void onInit() {
    PreferencesUtil.getInstance().init("cbl_survey_setting", this);

    mainLayout = (ViewFlipper) findViewById(R.id.mainContainer);



    tabViewOutLetSaleRep  = new TabViewOutletSaleRep(this);
    mainLayout.addView(tabViewOutLetSaleRep.getMainLayout());

    surveyResultListView = new SurveyResultListView(this);
    mainLayout.addView(surveyResultListView.getMainLayout());

    historyListView = new HistoryListView(this);
    mainLayout.addView(historyListView.getMainLayout());

    settingsView = new SettingsView(this, outletListView);
    mainLayout.addView(settingsView.getMainLayout());

    tabOutlet = (Button) findViewById(R.id.tabOutlet);
    tabOutlet.setOnClickListener(this);
    tabSync = (Button) findViewById(R.id.tabSync);
    tabSync.setOnClickListener(this);
    tabHistory = (Button) findViewById(R.id.tabHistory);
    tabHistory.setOnClickListener(this);
    tabSettings = (Button) findViewById(R.id.tabSettings);
    tabSettings.setOnClickListener(this);

    initSettings();
    initGPS();

}

private void initGPS() {
    gps = new GPSTracker(this);
    if(!gps.canGetLocation()){
        gps.showSettingsAlert(new GPSSettingButtonListener() {

            @Override
            public void onNegativeButtonClick() {

                finish();

            }
        });
    }
}

private void initSettings()
{
    String pwd = PreferencesUtil.getInstance().getString("pwd");
    if (pwd == null || pwd.equals(""))
    {
        pwd = Constant.DEFAULT_PASSWORD;
    }
    App.getInstance().setMasterPassword(pwd);

    String ip = PreferencesUtil.getInstance().getString("ip");
    if (ip == null || ip.equals(""))
    {
        ip = Constant.DEFAULT_IP;
    }
    App.getInstance().setIpAddress(ip);
    App.getInstance().setMainActivity(this);
}

@Override
protected void onAssignValues() {
    tabOutlet.setSelected(true);
    outletListView.assignValues();
}

@Override
protected void onResume() {
    super.onResume();

    mainLayout.post(new Runnable() {

        @Override
        public void run() {
            boolean activated = PreferencesUtil.getInstance().getLong("superviser_id") > 0;
            App.getInstance().setActivated(activated);
            if (!activated) {
                new ActivateApp(MainActivity.this).show();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_CANCELED) {
        surveyResultListView.assignValues();
    }
}

@Override
public void onClick(View view) {
    if (!view.isSelected()) {
        resetTabSelection();

        view.setSelected(true);

        onTabSelected(view);
    }
}

private void onTabSelected(View view) {
    isOutletScreen = false;
    if (view.equals(tabOutlet)) {
        mainLayout.setDisplayedChild(0);
        isOutletScreen = true;
    }
    else if (view.equals(tabSync)) {
        mainLayout.setDisplayedChild(1);
        surveyResultListView.assignValues();
    }
    else if (view.equals(tabHistory)) {

        Intent intent = new Intent(this, SurveySummaryActivity.class);

    }
    else if (view.equals(tabSettings)) {
        mainLayout.setDisplayedChild(3);
        settingsView.assignValues();
    }
}

public void onTabSwitch(int tab)
{
    switch (tab)
    {
    case 0:
        mainLayout.setDisplayedChild(0);
        isOutletScreen = true;
        tabOutlet.setSelected(true);
        break;
    case 1:
        mainLayout.setDisplayedChild(1);
        surveyResultListView.assignValues();
        tabSync.setSelected(true);
        break;
    case 2:
        mainLayout.setDisplayedChild(3);
        settingsView.assignValues();
        tabSettings.setSelected(true);
    }
}

public void resetTabSelection() {
    tabOutlet.setSelected(false);
    tabSync.setSelected(false);
    tabHistory.setSelected(false);
    tabSettings.setSelected(false);
}

public ViewFlipper getMainLayout() {
    return mainLayout;
}

public Button getTabOutlet() {
    return tabOutlet;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == 4) { // Keyback
        mainLayout.setDisplayedChild(0);
        resetTabSelection();
        tabOutlet.setSelected(true);
        if (isOutletScreen)
        {
            finish();
        }
        isOutletScreen = true;
    }
    return true;
}




}

像这样的TabViewOutletSaleRep代码

public class TabViewOutletSaleRep extends TabActivity{
private Activity activity;
private OutletListView outletListView;
private SaleRepListView saleRepListView;
private TabHost mainLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_outlet_salerep);

    TabHost tabHost = getTabHost(); 

    outletListView=new OutletListView(this);
    // Android tab
    Intent intentOutlet = new Intent().setClass(this, outletListView.getClass());
    TabSpec tabSpecOutlet = tabHost
        .newTabSpec("Outlet")
        .setIndicator("Outlet")
        .setContent(intentOutlet);

    saleRepListView=new SaleRepListView(this);
    // Apple tab
    Intent intentSaleRep = new Intent().setClass(this, saleRepListView.getClass());
    TabSpec tabSpecSaleRep = tabHost
        .newTabSpec("SaleRep")
        .setIndicator("SaleRep")
        .setContent(intentSaleRep);

    // add all tabs 
            tabHost.addTab(tabSpecOutlet);
            tabHost.addTab(tabSpecSaleRep);

            //set Windows tab as default (zero based)
            tabHost.setCurrentTab(1);
}

public TabViewOutletSaleRep(Activity acticity){
    this.activity=acticity;
    mainLayout = (TabHost) LayoutInflater.from(activity).inflate(R.layout.tab_outlet_salerep, null, false);
}

public View getMainLayout(){
    return mainLayout;
}
}

在像这样的AndroidManifest.xml代码中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cbl.survey"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Light.NoTitleBar" 
    android:name="App">

    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.apache.core.androidv2.feature.camera.CameraActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar" 
        android:screenOrientation="landscape"/>

    <activity android:name=".activity.OutletDetailActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>

    <activity android:name=".activity.SurveyActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>

    <activity android:name=".activity.SurveyResultActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>

     <activity android:name=".activity.GalleryActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>

     <activity android:name=".activity.ResultSyncActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>

     <activity android:name=".activity.SurveySummaryActivity"  
        android:theme="@android:style/Theme.Light.NoTitleBar"/>
     <activity android:name=".activity.view.TabViewOutletSaleRep"
         android:theme="@android:style/Theme.Light.NoTitleBar"/>    
</application>

</manifest>

我尝试将tabhost插入ViewFlipperin MainActivity。当我尝试在ViewFlipper中添加view作为tabhost时,会发生错误并显示logcat。

 05-23 00:48:19.541: E/AndroidRuntime(883): FATAL EXCEPTION: main
 05-23 00:48:19.541: E/AndroidRuntime(883): Process: com.cbl.survey, PID: 883
 05-23 00:48:19.541: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.cbl.survey/com.cbl.survey.activity.MainActivity}: java.lang.NullPointerException
 05-23 00:48:19.541: E/AndroidRuntime(883):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.os.Looper.loop(Looper.java:136)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-23 00:48:19.541: E/AndroidRuntime(883):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 00:48:19.541: E/AndroidRuntime(883):  at java.lang.reflect.Method.invoke(Method.java:515)
05-23 00:48:19.541: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-23 00:48:19.541: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-23 00:48:19.541: E/AndroidRuntime(883):  at dalvik.system.NativeStart.main(Native Method)
05-23 00:48:19.541: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
05-23 00:48:19.541: E/AndroidRuntime(883):  at com.cbl.survey.activity.MainActivity.onAssignValues(MainActivity.java:117)
05-23 00:48:19.541: E/AndroidRuntime(883):  at com.apache.core.androidv2.activity.BaseActivity.onCreate(BaseActivity.java:42)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.Activity.performCreate(Activity.java:5231)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-23 00:48:19.541: E/AndroidRuntime(883):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-23 00:48:19.541: E/AndroidRuntime(883):  ... 11 more

1 个答案:

答案 0 :(得分:1)

outletListView中的MainActivit永远不会被初始化