该应用程序崩溃,无法启动。代码编译没有错误,但是一旦我运行模拟器尝试测试应用程序就会崩溃。任何帮助都将不胜感激!
这是我的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
答案 0 :(得分:4)
将此初始化程序段移至活动中的onCreate()
:
{
// Initialization
...
}
实例初始化(stacktrace中的<init>
)为时过早,无法调用findViewById()
等活动方法。
由于您还没有onCreate()
并且需要一个,因此您可以将初始化程序转换为一个。用
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout_id);
// Initialization
...
其中some_layout_id
是指您希望用作此活动的内容视图的XML布局。