首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException findViewById()

NullPointerException findViewById()
EN

Stack Overflow用户
提问于 2013-04-11 00:34:54
回答 3查看 854关注 0票数 0

我有个大问题。

在我的三星Galaxy S2安卓4.2.2上,一切看起来都很好,但当我在我的Galaxy Tab安卓4.0.2上启动它时,它抛出了一个findViewById(R.mainn.seite)的NullPointerException。

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

伙计们,你们知道吗?

代码语言:javascript
复制
@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文件如下所示:

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-11 00:39:33

ids应该是R.id.something...并且应该在xml中指定为@+id/某物。另外,你在R.mainn.seite中有一个拼写错误

可能的bug来源是布局选择器,请检查您的活动/片段是否有多个布局。

票数 1
EN

Stack Overflow用户

发布于 2013-04-11 00:38:08

应该是(R.main.seite) main而不是main

票数 0
EN

Stack Overflow用户

发布于 2017-12-01 16:17:53

为了从XML中膨胀视图,布局膨胀器会搜索"@+id".If,它不会找到和膨胀任何东西,因此会将null返回给对象引用.....

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15931531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档