关于getview重复调用的输出为NULL问题?

Android-android getViewById() NullPointerException
Results 1 to 1 of 1
Bookmark & Share
Linear Mode
Senior Member
Android-android getViewById() NullPointerException
I'm trying to make my app changes its layout when user rotate the screen.When i launch the app while the screen of virtual machines is in portrait position.The app is ok when launch but when I rotate the screen to landscape, the app will stops and the logCat shows a nullPointerException at line 40
line 39 btn=(Button)findViewById(R.id.btn);
line 40 btn.setOnClickListener(btnListener);
I think the findViewById() method is returning null, but i dont know why.Also, if i launch the app while screen in landscape position, the same error occured.
Here are the codes:
MainActivity.java
package com.example.import android.os.Bimport android.app.Aimport android.app.AlertDimport android.app.AlertDialog.Bimport android.app.Dimport android.content.DialogIimport android.content.SharedPimport android.content.res.Cimport android.view.Mimport android.view.MenuIimport android.view.Vimport android.widget.Bimport android.widget.Tpublic class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preference = getSharedPreferences(&preferences&, MODE_PRIVATE);
if(getResources().getConfiguration().orientation == 1)
if(preference.getInt(&THE_CHECKED&, 0) == 0)
setContentView(R.layout.portrait_black);
else if(preference.getInt(&THE_CHECKED&, 0) == 1)
setContentView(R.layout.portrait_white);
else if(getResources().getConfiguration().orientation == 2)
if(preference.getInt(&THE_CHECKED&, 0) == 0)
setContentView(R.layout.landscape_black);
else if(preference.getInt(&THE_CHECKED&, 0) == 1)
setContentView(R.layout.landscape_white);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(btnListener);
private Button.OnClickListener btnListener = new Button.OnClickListener()
public void onClick(View v)
Toast toast1 = Toast.makeText(MainActivity.this, &HI&, Toast.LENGTH_LONG);
toast1.show();
private DialogInterface.OnClickListener theDialogListener = new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
SharedPreferences.Editor editor = preference.edit();
switch(which)
case 0: //Black
editor.putInt(&THE_CHECKED&, 0);
case 1: //White
editor.putInt(&THE_CHECKED&, 1);
((Dialog)dialog).dismiss();
onCreate(new Bundle());
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
public boolean onOptionsItemSelected(MenuItem item)
SharedPreferences.Editor editor = preference.edit();
switch (item.getItemId())
case R.id.theme:
Builder theDialog = new AlertDialog.Builder(this);
theDialog.setTitle(&Theme&);
theDialog.setSingleChoiceItems(R.array.theme_menu, preference.getInt(&THE_CHECKED&, 0), theDialogListener);
theDialog.show();
return super.onOptionsItemSelected(item);
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
onCreate(new Bundle());
portrait_white.xml
portrait_black.xml
landscape_white.xml
landscape_black.xml
LogCat(when i launch the app while screen is in landscape position)
02-21 01:26:53.895: D/AndroidRuntime(1836): Shutting down VM02-21 01:26:53.925: W/dalvikvm(1836): threadid=1: thread exiting with uncaught exception (group=0x-21 01:26:53.935: E/AndroidRuntime(1836): FATAL EXCEPTION: main02-21 01:26:53.935: E/AndroidRuntime(1836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread.performLaunchActivity(A
ctivityThread.java: 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread.handleLaunchActivity(Ac
tivityThread.java: 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread.access$600(ActivityThre
ad.java:141)02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread$H.handleMessage(Activit
yThread.java: 01:26:53.935: E/AndroidRuntime(1836):
at android.os.Handler.dispatchMessage(Handler.java:99
)02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.os.Looper.loop(Looper.java:137)02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread.main(ActivityThread.jav
a: 01:26:53.935: E/AndroidRuntime(1836):
at java.lang.reflect.Method.invokeNative(Native Method)02-21 01:26:53.935: E/AndroidRuntime(1836):
at java.lang.reflect.Method.invoke(Method.java:525)02-21 01:26:53.935: E/AndroidRuntime(1836):
at com.android.internal.os.ZygoteInit$MethodAndArgsCa
ller.run(ZygoteInit.java:737)02-21 01:26:53.935: E/AndroidRuntime(1836):
at com.android.internal.os.ZygoteInit.main(ZygoteInit
.java:553)02-21 01:26:53.935: E/AndroidRuntime(1836):
at dalvik.system.NativeStart.main(Native Method)02-21 01:26:53.935: E/AndroidRuntime(1836): Caused by: java.lang.NullPointerException02-21 01:26:53.935: E/AndroidRuntime(1836):
at com.example.test.MainActivity.onCreate(MainActivit
y.java:40)02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.app.Activity.performCreate(Activity.java:5
133)02-21 01:26:53.935: E/AndroidRuntime(1836):
at android.app.Instrumentation.callActivityOnCreate(I
nstrumentation.java: 01:26:53.935: E/AndroidRuntime(1836):
at android.app.ActivityThread.performLaunchActivity(A
ctivityThread.java: 01:26:53.935: E/AndroidRuntime(1836):
... 11 moreThanks.
By bigdata in forum Android Applications
By bigdata in forum Android Applications
By bigdata in forum Android Applications
By bigdata in forum Android Applicationsjava的一个问题,下面的程序为什么输出0.0和null呢?_百度知道
java的一个问题,下面的程序为什么输出0.0和null呢?
提问者采纳
恩,因为你的class Book哗範糕既蕹焕革唯宫沥{}没有构造方法,所以java默认把你的float price,设置成0.0,String I设置成null,所以你的输出结果是0.0 null。
如果你的这个方法public book(){price=1.1;Isbought=&shi&;}的方法名字的首字母改成大写的B,输出结果就是1.1 shi了。
其他类似问题
为您推荐:
null的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁5425人阅读
android(83)
作者同类文章X
adapter的getview(int position, View convertView, ViewGroup parent)的缓存比较多,比如一页可以显示8个,0-7。那么向下滑动2个之后在getview只会有position8,和9,说明其他的调用的其他的缓存,如果按照一般拖动的实现,应该将上一次的图像生成个bitmap,具体还要看看源代码吧,还有convertView是否为null的规律
if(convertView == null) {
Log.d(TAG, &position&&&+position+&&&convertivew null&);
FrameLayout layout = new FrameLayout(TestAdapterPosition.this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, 60);
TextView tt = new TextView(TestAdapterPosition.this);
layout.addView(tt, lp);
convertView =
item = new Item();
convertView.setTag(item);
Log.d(TAG, &position&&&+position+&&&convertivew is not null&);
item = (Item) convertView.getTag();
///--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
说一下一个需求的解决,自绘的tab共有一个listview,要求每个tab记住listview已经scrolly位置,tab切回来的时候要还原位置。
从listview中取得adapter item的position好像比较方便,但是想随意取出对应position的view,从mListview.getSelectedView()靠谱,mAdapter.getView(int position, View convertView, ViewGroup parent)应该是对应的,但是位置都显示为0,最后mListview.getChildAt(0)计算偏移位置位置,配合mListview.getFirstVisiblePosition()记住position。还有一直没太搞懂这个适配器模式,我认为getchildAt()在listview不太好使呢,这里要注意的是getChildAt(0),它的0是从第一个可视的开始算,总共也就可视的那几个,超过了就返回为null了。代码如下:
Rect rect = new Rect();
if(mListview.getChildAt(0) != null) {
mListview.getChildAt(0).getHitRect(rect);
mTabScrollY[tab] = rect.
mTabPosition[tab] = mListview.getFirstVisiblePosition();在tab切回来的时候重新定位
mListview.setSelectionFromTop(mTabPostion[tab], mTabScrollY[tab]);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:224973次
积分:3238
积分:3238
排名:第5630名
原创:90篇
转载:51篇
评论:29条
(5)(13)(4)(4)(10)(3)(11)(2)(6)(6)(8)(3)(10)(5)(7)(5)(9)(3)(2)(19)(9)(1)

我要回帖

更多关于 getview position 0 的文章

 

随机推荐