gpt4 book ai didi

java - 应用程序无法启动-空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:26:51 25 4
gpt4 key购买 nike

应用程序崩溃且无法启动。代码编译时没有错误,但当我运行模拟器尝试测试应用程序时就会崩溃。任何帮助将不胜感激!

这是我的 MainActivity.java

package com.overworldinnovations.datatool;

import info.datatool.tabsswipe.adapter.TabsPagerAdapter;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Convert", "Data Ranges", "" };
{

// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}

这是我尝试运行应用程序并崩溃后的 CatLog

09-14 17:48:20.364: E/AndroidRuntime(1168): FATAL EXCEPTION: main
09-14 17:48:20.364: E/AndroidRuntime(1168): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.os.Looper.loop(Looper.java:137)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 17:48:20.364: E/AndroidRuntime(1168): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 17:48:20.364: E/AndroidRuntime(1168): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 17:48:20.364: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 17:48:20.364: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 17:48:20.364: E/AndroidRuntime(1168): at dalvik.system.NativeStart.main(Native Method)
09-14 17:48:20.364: E/AndroidRuntime(1168): Caused by: java.lang.NullPointerException
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.Activity.findViewById(Activity.java:1839)
09-14 17:48:20.364: E/AndroidRuntime(1168): at com.overworldinnovations.datatool.MainActivity.<init>(MainActivity.java:25)
09-14 17:48:20.364: E/AndroidRuntime(1168): at java.lang.Class.newInstanceImpl(Native Method)
09-14 17:48:20.364: E/AndroidRuntime(1168): at java.lang.Class.newInstance(Class.java:1319)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
09-14 17:48:20.364: E/AndroidRuntime(1168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-14 17:48:20.364: E/AndroidRuntime(1168): ... 11 more
09-14 17:53:20.504: I/Process(1168): Sending signal. PID: 1168 SIG: 9

最佳答案

将此初始化 block 移至 onCreate()在您的 Activity 中:

{

// Initialization

...
}

实例初始化(堆栈跟踪中的 <init>)还为时过早,无法调用 Activity 方法,例如 findViewById() .

<小时/>

因为您还没有 onCreate()并且需要一个,你可以将初始化器变成一个。更换开口{类似的东西

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.some_layout_id);

// Initialization

...

哪里some_layout_id指您要用作此 Activity 的内容 View 的 XML 布局。

关于java - 应用程序无法启动-空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com