2013-04-10 167 views
0

我有一个很大的问题。NullPointerException异常findViewById()

在我的三星Galaxy S2的Android 4.2.2一切都看起来不错,但是当我开始在我的的Galaxy Tab的Android 4.0.2,它抛出通过findViewById(R.mainn.seite)一个NullPointerException

findViewById(R。 mainn.seite)总是返回null。这怎么可能? 我重建我的应用程序几次,但没有解决问题。

任何想法的家伙?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ExceptionHandler.register(this, "http://cdu-viersen.de/err/server.php"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 



    Intent i = getIntent(); 
    if(i.getIntExtra("von", -1)==1){ 
     update(); 
    } 
    else if(i.getIntExtra("von", -1)==2){ 
     einstellungen(); 
    } 

    else{ 
     checkLaufzeit(); 

    } 
    if (getIntent().getBooleanExtra("EXIT", false)) { 
     finish(); 
    } 

    ImageView wissen = (ImageView) findViewById(R.main.wissen); 
    wissen.setOnClickListener(choose); 
    ImageView dia = (ImageView) findViewById(R.main.diagnostik); 
    dia.setOnClickListener(choose); 
    ImageView klinik = (ImageView) findViewById(R.main.klinik); 
    klinik.setOnClickListener(choose); 
    ImageView medi = (ImageView) findViewById(R.main.medikamente); 
    medi.setOnClickListener(choose); 
    ImageView rechner = (ImageView) findViewById(R.main.rechner); 
    rechner.setOnClickListener(choose); 
    ImageView notfall = (ImageView) findViewById(R.main.notfall); 
    notfall.setOnClickListener(choose); 

    TextView seite = (TextView) findViewById(R.mainn.seite); 
    seite.setOnTouchListener(new MyTouchListener()); 

} 

我的XML文件是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/bg_main" 
android:id="@+main/main" 
tools:context=".Main" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="5dp" 
    android:id="@+main/test"> 
    <ImageView 
    android:layout_width="16dp" 
    android:layout_height="70dp" 
    android:layout_marginTop="47dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:src="@drawable/icon_rand" 
    android:id="@+main/mmmm"/> 

    <ImageView 
    android:layout_width="16dp" 
    android:layout_height="70dp" 
    android:layout_alignParentLeft="true" 
    android:layout_marginTop="17dp" 
    android:layout_below="@main/mmmm" 
    android:src="@drawable/icon_rand"/> 
    <ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+main/wissen" 
    android:layout_toRightOf="@main/mmmm" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/icon_wissen"/> 
    <ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+main/medikamente" 
    android:layout_toRightOf="@main/mmmm" 
    android:layout_marginTop="5dp" 
    android:layout_below="@main/wissen" 
    android:src="@drawable/icon_medikamente"/> 
    <ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+main/rechner" 
    android:layout_toRightOf="@main/mmmm"  
    android:layout_marginTop="5dp" 
    android:layout_below="@main/medikamente" 
    android:src="@drawable/icon_rechner"/> 

    <ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+main/klinik" 
    android:layout_toRightOf="@main/wissen" 
    android:layout_marginTop="47dp" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/icon_klinik"/> 

    <ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+main/diagnostik" 
    android:layout_toRightOf="@main/wissen" 
    android:layout_marginTop="5dp" 
    android:layout_below="@main/klinik" 
    android:src="@drawable/icon_diagnostik"/> 
    <TextView 
    android:layout_width="40dp" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:background="@android:color/transparent" 
    android:id="@+mainn/seite"/> 

</RelativeLayout> 



<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+main/main_n" 
    android:visibility="gone" 
    android:background="@drawable/hintergrund_main_links"> 

    <TextView 
    android:layout_width="40dp" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:background="@android:color/transparent" 
    android:id="@+main/seite_n"/> 


    <ImageView 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="47dp" 
     android:layout_marginRight="-25dp" 
     android:src="@drawable/icon_leer" 
     android:id="@+main/halbrechts"/> 

    <ImageView 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_alignParentRight="true" 
     android:layout_below="@main/halbrechts" 
     android:layout_marginTop="17dp" 
     android:layout_marginRight="-25dp" 
     android:src="@drawable/icon_leer"/> 




    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="58dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:orientation="vertical" 
     android:id="@+main/q"> 

     <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:src="@drawable/dienstplan_icon" 
     android:id="@+main/dienstplan_n"/> 


     <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/update_icon" 
     android:id="@+main/update_n"/> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="18dp" 
     android:orientation="vertical" 
     android:layout_toRightOf="@main/q" 
     android:layout_alignParentBottom="true" 
     android:id="@+main/qq"> 

     <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:src="@drawable/hintergrund_icon" 
     android:id="@+main/hintergrund_n"/> 


     <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_marginTop="5dp"  
     android:src="@drawable/einstellungen_icon" 
     android:id="@+main/einstellungen_n"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="58dp" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" 
     android:layout_toRightOf="@main/qq"> 

     <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:src="@drawable/foto_icon" 
     android:id="@+main/foto_n"/> 

    </LinearLayout> 

    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="13dp" 
    android:background="@drawable/leiste_unten" 
    android:layout_alignParentBottom="true" 
    /> 


</RelativeLayout> 







<ImageView 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:id="@+main/notfall" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/icon_notfall" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp"/> 

<ImageView 
    android:layout_width="210dp" 
    android:layout_height="53dp" 
    android:src="@drawable/schriftzug" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginBottom="25dp"/> 



<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="13dp" 
    android:background="@drawable/leiste_unten" 
    android:layout_alignParentBottom="true" 
    /> 



</RelativeLayout> 
+0

当你总是使用主要为您的项目其余为什么你命名的项目mainn/seite? – DigCamara 2013-04-10 16:39:29

+0

只是用于测试的原因,如果有一个与主/ seite一个问题... 最近我发现,这是一个普遍的问题平板电脑。 该应用程序不会在手机上崩溃,但在每次尝试的平板电脑上都会崩溃。 任何想法? – Chrissss 2013-04-10 17:11:29

回答

1

的ID应该是R.id.something ...并应该在xml中指定为@ + id/something。 除了你在R.mainn.seite有一个错字

一个可能的错误来源是布局选择器,检查你的活动/片段是否有多个布局。

+0

感谢您的重播。 ID被R.id ....他们被指定为@ + ID /东西... 你是什么意思的布局选择? 我有两个不同的RelativeLayouts内相互堆叠1布局xml。只有一个是可见的另一个走了 – Chrissss 2013-04-10 17:06:43

+1

你是对的... 我有不同的屏幕尺寸不同的布局和我已经忘记了更新这些适用于平板电脑! – Chrissss 2013-04-10 17:14:27

0

它应该是(R.main.seite)主不miann

+0

由于测试的原因,我打电话给id mainn ...那不是一个错字:/ – Chrissss 2013-04-10 17:10:04

0

对于从XML中扩展视图,布局Inflator会搜索“@ + id”。如果您要提供其他内容,则不会去查找并膨胀任何内容,因此将返回null给您的对象引用.....