错误ava.lang.RuntimeException: Unable to start activity ComponentInfo{*}java.lang.NullPointerException

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/stven_king/article/details/37774131

这几天学习Android出现好多次这样的错误。

网上查阅了很多原因,大概有四种重要可能的原因:

原因一:xxx的错误,若为R.layout.activity_main  那么应该是acitvity_main.xml文件中的标签 使用错误,最常见的而且编译器不会提示的错误就是 android:name 和 android:id 两者混淆,仔细检查main.xml的标签是否全部正确

原因二:在setContentView(view)方法之后使用了requestWindowFeature()方法,并且在此错误下面会提示requestFeature必须在setContentView之前使用,只需要把requestWindowFeature()方法放在setContentView(view)方法之前就可以解决

原因三:在onCreate()方法之外,并且不属于任何一个方法体内直接给某控件findById(R.id.xx)所导致,需要在某方法内并且在setContentView(view)方法之前进行findById(R.id.xx)即可解决

原因四:在setContentView(view)之前没有对view进行实例化,只进行了声明而直接 setContentView(view) 所导致,仔细检查view是否setContentView(view)调用之前并在方法内进行实例化即可解决

原因五:因为发生了空指针的错误异常,所以你应该检查一下比如 自己定义Intent传递的类的实例 或者 list 这类的东西里面是否有值存在。

总结:自己感觉这种错误好像都是在某个地方没有实例化某个对象,要不就是实例化的范围没有与使用的范围在同一范围下。

阅读更多

#(Unable to start activity ComponentInfoXXX: java.lang.NullPointerException)#

07-22

新人求助!!!在调试的时候,Exception显示的错误是:rnrnjava.lang.RuntimeException: Unable to start activity ComponentInfocom.andyidea.tabdemo/com.andyidea.tabdemo.Item_java: java.lang.NullPointerExceptionrnrnrnrn怎么解决啊?求助啊!!有没有人帮我啊!!谢谢各位了!!!rnrn代码如下:rnrn[code=java]public class BActivity extends ActivityGrouprn rn private ViewPager viewPager;//页卡内容rn private ImageView imageView;// 动画图片rn private Button button3,button4,button5,button6;rn private List views;// Tab页面列表rn private int offset = 0;// 动画图片偏移量rn private int currIndex = 0;// 当前页卡编号rn private int bmpW;// 动画图片宽度rn private View view1,view2,view3,view4;//各个页卡rn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.bmain);rn InitImageView();rn InitButtonView();rn InitViewPager();rn rn rn private void InitViewPager() rn viewPager=(ViewPager) findViewById(R.id.Bviewpager);rn tryrn view1 = getLocalActivityManager().startActivity("MainActivity",rn new Intent(this, Item_java.class)).getDecorView(); //这个地方,try的时候无法通过。就在这里跳到catch了。rn view2 = getLocalActivityManager().startActivity("MainActivity",rn new Intent(this, Item_dotnet.class)).getDecorView();rn view3 = getLocalActivityManager().startActivity("MainActivity",rn new Intent(this, Item_insertapp.class)).getDecorView();rn view4 = getLocalActivityManager().startActivity("MainActivity",rn new Intent(this, Item_netmanagement.class)).getDecorView();rn views=new ArrayList();rn views.add(view1);rn views.add(view2);rn views.add(view3);rn views.add(view4);rn viewPager.setAdapter(new MyViewPagerAdapter(views));rn viewPager.setCurrentItem(0);rn viewPager.setOnPageChangeListener(new MyOnPageChangeListener());rn rn catch (Exception e) rn // TODO: handle exceptionrn int xx=1;rn rnrn rn /**rn * 初始化头标rn */rnrn private void InitButtonView() rn button3 = (Button) findViewById(R.id.btn_java);rn button4 = (Button) findViewById(R.id.btn_dotnet);rn button5 = (Button) findViewById(R.id.btn_insertapp);rn button6 = (Button) findViewById(R.id.btn_netmanagement);rn button3.setOnClickListener(new MyOnClickListener(0));rn button4.setOnClickListener(new MyOnClickListener(1));rn button5.setOnClickListener(new MyOnClickListener(2));rn button6.setOnClickListener(new MyOnClickListener(3));rn rnrn /**rn 2 * 初始化动画,这个就是页卡滑动时,下面的横线也滑动的效果,在这里需要计算一些数据rn 3 */rnrn private void InitImageView() rn imageView= (ImageView) findViewById(R.id.Bcursor);rn bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 获取图片宽度rn DisplayMetrics dm = new DisplayMetrics();rn getWindowManager().getDefaultDisplay().getMetrics(dm);rn int screenW = dm.widthPixels;// 获取分辨率宽度rn offset = (screenW /4 - bmpW) / 2;// 计算偏移量rn Matrix matrix = new Matrix();rn matrix.postTranslate(offset, 0);rn imageView.setImageMatrix(matrix);// 设置动画初始位置rn rn /** rn * rn * 头标点击监听 3 */rn private class MyOnClickListener implements OnClickListenerrn private int index=0;rn public MyOnClickListener(int i)rn index=i;rn rn public void onClick(View v) rn viewPager.setCurrentItem(index);rn rn rn public class MyViewPagerAdapter extends PagerAdapterrn private List mListViews;rn rn public MyViewPagerAdapter(List mListViews) rn this.mListViews = mListViews;rn rnrn @Overridern public void destroyItem(ViewGroup container, int position, Object object) rn container.removeView(mListViews.get(position));rn rnrnrn @Overridern public Object instantiateItem(ViewGroup container, int position) rn container.addView(mListViews.get(position), 0);rn return mListViews.get(position);rn rnrn @Overridern public int getCount() rn return mListViews.size();rn rn rn @Overridern public boolean isViewFromObject(View arg0, Object arg1) rn return arg0==arg1;rn rn rn public class MyOnPageChangeListener implements OnPageChangeListenerrnrn int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 rn int two = one * 2;// 页卡1 -> 页卡3 偏移量 rn int three = one * 3;// 页卡1 -> 页卡4偏移量rn public void onPageSelected(int arg0) rnrn Animation animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);rn currIndex = arg0;rn animation.setFillAfter(true);// True:图片停在动画结束位置rn animation.setDuration(300);rn imageView.startAnimation(animation);rnrn rn @Overridern public void onPageScrollStateChanged(int arg0) rn rn @Overridern public void onPageScrolled(int arg0, float arg1, int arg2) rn rnrnrnrnrn[/code]rnrnrnrnrnrn谢谢大家!!!!!!!!!!!!!!!

RuntimeException Unable to start activity ComponentInfo NullPointerException

09-09

我从另一个Activity的fragement里通过:rn Intent intent = new Intent(getActivity(), PlayerActivity.class);rn intent.putExtra("vid", (String) item.get("vId"));rn getActivity().startActivity(intent);rn跳转到PlayerActivity。然后运行到PlayerActivity中onCreate方法的第一句super.onCreate(savedInstanceState)时报错。debug时,savedInstanceState的值是null。我直接从主界面MainActivity中跳转到PlayerActivity也报同样的错误。这是怎么回事呢?这个PlayerActivity是用了优酷开源解码器的SDK,之前用着还没问题,然后我直接把它复制到这个工程了...[img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/4.gif][/img]rnrn09-09 01:22:40.905: E/AndroidRuntime(19765): FATAL EXCEPTION: mainrn09-09 01:22:40.905: E/AndroidRuntime(19765): Process: com.uker, PID: 19765rn[color=#FF0000]09-09 01:22:40.905: E/AndroidRuntime(19765): java.lang.RuntimeException: Unable to start activity ComponentInfocom.uker/com.uker.ui.PlayerActivity: java.lang.NullPointerException[/color]rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread.access$800(ActivityThread.java:135)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.os.Handler.dispatchMessage(Handler.java:102)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.os.Looper.loop(Looper.java:136)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread.main(ActivityThread.java:5017)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at java.lang.reflect.Method.invokeNative(Native Method)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at java.lang.reflect.Method.invoke(Method.java:515)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at dalvik.system.NativeStart.main(Native Method)rn[color=#FF0000]09-09 01:22:40.905: E/AndroidRuntime(19765): Caused by: java.lang.NullPointerException[/color]rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.youku.player.util.PlayerUiUtile.initDex(PlayerUiUtile.java:85)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.youku.player.util.PlayerUiUtile.refreshMediaplayerDelegate(PlayerUiUtile.java:145)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.youku.player.ui.interf.IBaseActivity.onCreate(IBaseActivity.java:62)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at com.youku.player.base.YoukuBasePlayerActivity.onCreate(YoukuBasePlayerActivity.java:187)rn[color=#FF0000]09-09 01:22:40.905: E/AndroidRuntime(19765): at com.uker.ui.PlayerActivity.onCreate(PlayerActivity.java:48)[/color]rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.Activity.performCreate(Activity.java:5231)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)rn09-09 01:22:40.905: E/AndroidRuntime(19765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)rn09-09 01:22:40.905: E/AndroidRuntime(19765): ... 12 more

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

08-16

进入activity后退出,再次点进去程序就奔溃了。不知道什么原因,求大神解答,谢谢!奔溃日志:rnE/AndroidRuntime: FATAL EXCEPTION: mainrn Process: com.hks360.car_treasure, PID: 27293rn java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.hks360.car_treasure/com.hks360.car_treasure.activity.TrackActivity: java.lang.ClassNotFoundException: Didn't find class "com.hks360.car_treasure.activity.TrackActivity" on path: DexPathList[[zip file "/data/app/com.hks360.car_treasure-2/base.apk"],nativeLibraryDirectories=[/data/app/com.hks360.car_treasure-2/lib/arm64, /data/app/com.hks360.car_treasure-2/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]rn at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)rn at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773)rn at android.app.ActivityThread.-wrap11(ActivityThread.java)rn at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1587)rn at android.os.Handler.dispatchMessage(Handler.java:111)rn at android.os.Looper.loop(Looper.java:207)rn at android.app.ActivityThread.main(ActivityThread.java:5940)rn at java.lang.reflect.Method.invoke(Native Method)rn at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)rn at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790)rn Caused by: java.lang.ClassNotFoundException: Didn't find class "com.hks360.car_treasure.activity.TrackActivity" on path: DexPathList[[zip file "/data/app/com.hks360.car_treasure-2/base.apk"],nativeLibraryDirectories=[/data/app/com.hks360.car_treasure-2/lib/arm64, /data/app/com.hks360.car_treasure-2/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]rn at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:511)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:469)rn at android.app.Instrumentation.newActivity(Instrumentation.java:1079)rn at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2603)rn at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773) rn at android.app.ActivityThread.-wrap11(ActivityThread.java) rn at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1587) rn at android.os.Handler.dispatchMessage(Handler.java:111) rn at android.os.Looper.loop(Looper.java:207) rn at android.app.ActivityThread.main(ActivityThread.java:5940) rn at java.lang.reflect.Method.invoke(Native Method) rn at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929) rn at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790) rn Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.hks360.car_treasure.activity.TrackActivity" on path: DexPathList[[zip file "/data/user/0/com.hks360.car_treasure/app_curRes/BaiduNaviSDK_3.1.1.jar"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]rn at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:511)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:504)rn ... 12 morern Suppressed: java.lang.ClassNotFoundException: com.hks360.car_treasure.activity.TrackActivityrn at java.lang.Class.classForName(Native Method)rn at java.lang.BootClassLoader.findClass(ClassLoader.java:781)rn at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:504)rn ... 13 morern Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace availablernrn

没有更多推荐了,返回首页