无效的新的setText属性的Nullpointer异常

时间:2015-07-06 16:55:21

标签: java android nullpointerexception null

这只是我的应用中一项活动的代码,它基本上是做什么的,具体取决于它提供了一个紧急情况下联系人的列表。但是,我最近收到了一些要添加到应用程序的新数据,现在我不得不在布局中添加更多按钮(最初只有2个按钮),并且由于某种原因,我得到了一个nullpointer异常。我添加的新按钮。因为我相信我已经正确地初始化按钮,为什么我得到这样的例外是没有意义的。任何帮助将不胜感激!

Contacts.java

<button class="btn btn-default btn-flat btn-xs comment-edit" type="button" 
   data-toggle="modal" class="fa fa-edit" title="edit" data-target="#ModalDiv">
     Button Name
</button>



<div class="modal fade in" id="comment-modal" tabindex="-1" role="dialog" 
       aria-labelledby="Modal" aria-hidden="false" style="display: block;" id="ModalDiv">
... content
</div>

activity_contacts.xml

import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
import android.widget.RelativeLayout;

public class Contacts extends ActionBarActivity {

    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;
    Button bt5;
    Button bt6;

    TextView t1;
    TextView t2;
    TextView t3;
    TextView t4;
    TextView t5;
    TextView t6;

    private String num1;
    private String num2;
    private String num3;
    private String num4;
    private String num5;
    private String num6;
   // RelativeLayout myLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String branch = extras.getString("branch");
        String level = extras.getString("level");
        bt1 = (Button) findViewById(R.id.contact_button1);
        bt2 = (Button) findViewById(R.id.contact_button2);
        bt3 = (Button) findViewById(R.id.contact_button3);
        bt4 = (Button) findViewById(R.id.contact_button4);
        bt5 = (Button) findViewById(R.id.contact_button5);
        bt6 = (Button) findViewById(R.id.contact_button6);
        t1 = (TextView) findViewById(R.id.textView1);
        t2 = (TextView) findViewById(R.id.textView2);
        t3 = (TextView) findViewById(R.id.textView3);
        t4 = (TextView) findViewById(R.id.textView4);
        t5 = (TextView) findViewById(R.id.textView5);
        t6 = (TextView) findViewById(R.id.textView6);

        if(branch.contentEquals("Hoody") && level.contentEquals("Safety In Charge"))
        {
            bt1.setText("Javaji P");
            bt2.setText("H S Rao");
            bt3.setText("Sunil KB");
            bt4.setText("K Kishor");
            bt5.setText("Vincent");
            bt6.setText("Sreenivas K");
            num1 = "+919845956099";
            num2 = "+919686198661";
            num3 = "+919880141599";
            num4 = "+919880185109";
            num5 = "+919844303344";
            num6 = "+919880598205";

        }
        if(branch.contentEquals("Hoody") && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
           // t1.setVisibility(View.GONE);
          //  t2.setVisibility(View.GONE);
           /// t3.setVisibility(View.GONE);
          //  t4.setVisibility(View.GONE);
        //    t5.setVisibility(View.GONE);
         //   t6.setVisibility(View.GONE);
            num1 = "08022979333";
        }
        if(branch.contentEquals("Hoody") && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("B S Lokeshwaran(Head-Admin)");
            bt2.setText("Philip Mammen(Head-HR)");
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
        //    t1.setVisibility(View.GONE);
         //   t2.setVisibility(View.GONE);
        //    t3.setVisibility(View.GONE);
        //    t4.setVisibility(View.GONE);
        //    t5.setVisibility(View.GONE);
        //    t6.setVisibility(View.GONE);
            num1 = "+919741144114";
            num2 = "+919880141779";

        }
        if(branch.contentEquals("BTP") && level.contentEquals("Safety In Charge"))
        {
            bt1.setText("Javaji P");
            bt2.setText("H S Rao");
            bt3.setText("Sunil KB");
            bt4.setText("K Kishor");
            bt5.setText("Roshan");
            bt6.setText("Varghese M");
            num1 = "+919845956099";
            num2 = "+919686198661";
            num3 = "+919880141599";
            num4 = "+919880185109";
            num5 = "+919945729472";
            num6 = "+919880114562";

        }
        if(branch.contentEquals("BTP") && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
            num1 = "08022984000";
        }
        if(branch.contentEquals("BTP") && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("B S Lokeshwaran(Head-Admin)");
            bt2.setText("Philip Mammen(Head-HR)");
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
            num1 = "+919741144114";
            num2 = "+919880141779";

        }
        if(branch.contentEquals("TD") && level.contentEquals("Safety In Charge"))
        {

            bt1.setText("Javaji P");
            bt2.setText("H S Rao");
            bt3.setText("Sunil KB");
            bt4.setText("K Kishor");
            bt5.setText("Anitha S");
            bt6.setText("Sreenivas K");
            num1 = "+919845956099";
            num2 = "+919686198661";
            num3 = "+919880141599";
            num4 = "+919880185109";
            num5 = "+919980299033";
            num6 = "+919880598205";

        }
        if(branch.contentEquals("TD") && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
            num1 = "08022979761";
        }
        if(branch.contentEquals("TD") && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("B S Lokeshwaran(Head-Admin)");
            bt2.setText("Philip Mammen(Head-HR)");
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
            num1 = "+919741144114";
            num2 = "+919880141779";

        }
        if((branch.contentEquals("Neyyar") && level.contentEquals("Safety In Charge")) || (branch.contentEquals("Leela") && level.contentEquals("Safety In Charge")) || (branch.contentEquals("Gayathri") && level.contentEquals("Safety In Charge")))
        {
            bt1.setText("Tony");
            bt2.setText("Jossy Joseph");
            num1 = "+919745818995";
            num2 = "+919961528687";

        }
        if((branch.contentEquals("Neyyar")||branch.contentEquals("Leela")|| branch.contentEquals("Gayathri")) && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            num1 = "04716661000";
        }
        if((branch.contentEquals("Neyyar")||branch.contentEquals("Leela")|| branch.contentEquals("Gayathri")) && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("Sreekumar.V(Centre Head)");
            bt2.setText("Ajish MJ(Head-HR)");
            num1 = "+919846022266";
            num2 = "+919895806075";

        }

        if(branch.contentEquals("Chennai") && level.contentEquals("Safety In Charge"))
        {
            bt1.setText("Meeran");
            bt2.setVisibility(View.GONE);
            num1 = "+919884515288";


        }
        if(branch.contentEquals("Chennai") && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            num1 = "08022979761";
        }
        if(branch.contentEquals("Chennai") && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("Jayendran.G(Center Head)");
            bt2.setVisibility(View.GONE);
            num1 = "+919840385745";


        }
        if((branch.contentEquals("Alpha 1 & 2") || branch.contentEquals("SEZ")) && level.contentEquals("Safety In Charge"))
        {
            bt1.setText("Laxman Jadhav");
            bt2.setVisibility(View.GONE);
            num1 = "+919689887857";

        }
        if((branch.contentEquals("Alpha 1 & 2") || branch.contentEquals("SEZ")) && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            bt3.setVisibility(View.GONE);
            bt4.setVisibility(View.GONE);
            bt5.setVisibility(View.GONE);
            bt6.setVisibility(View.GONE);
            num1 = "02066060033";
        }
        if((branch.contentEquals("Alpha 1 & 2") || branch.contentEquals("SEZ")) && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("Mr.Vasant Shah(Center Head)");
            bt2.setText("Ms.Simrit Dhindasa(Sr.Executive-HR)");
            num1 = "+919503838082";
            num2 = "+919921183320";

        }
        if(branch.contentEquals("Mumbai") && level.contentEquals("Safety In Charge"))
        {
            bt1.setText("Naveen Dighe");
            bt2.setVisibility(View.GONE);
            num1 = "+919821142863";

        }
        if(branch.contentEquals("Mumbai") && level.contentEquals("Emergency Declarer"))
        {
            bt1.setText("Nagarajan(Center Head)");
            bt2.setVisibility(View.GONE);
            num1 = "+919967723450";

        }
        if(branch.contentEquals("Mumbai") && level.contentEquals("Fire/Accident/Ambulance"))
        {
            bt1.setText("Security");
            bt2.setVisibility(View.GONE);
            num1 = "0226822145";
        }

    }

    public void launchDialer(String number)
    {
        String numberToDial = "tel:"+number;
        startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));
    }

    public void onClickbt1(View view)
    {
    launchDialer(num1);
    }

    public void onClickbt2(View view)
    {
        launchDialer(num2);
    }

    public void onClickbt3(View view)
    {
        launchDialer(num3);
    }

    public void onClickbt4(View view)
    {
        launchDialer(num4);
    }

    public void onClickbt5(View view)
    {
        launchDialer(num5);
    }

    public void onClickbt6(View view)
    {
        launchDialer(num6);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_contacts, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Logcat -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.prashanth.emergencycontacts.Contacts"
    android:background="@drawable/bg"
    android:id="@+id/layout1">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 1"
        android:id="@+id/contact_button1"
        android:onClick="onClickbt1"
        android:background="@android:drawable/dialog_holo_light_frame"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 2"
        android:id="@+id/contact_button2"
        android:layout_marginTop="40dp"
        android:onClick="onClickbt2"
        android:background="@android:drawable/dialog_holo_light_frame"
        android:layout_below="@+id/contact_button1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignLeft="@+id/contact_button1"
        android:layout_alignStart="@+id/contact_button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 3"
        android:id="@+id/contact_button3"
        android:onClick="onClickbt3"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/contact_button2"
        android:layout_alignLeft="@+id/contact_button2"
        android:layout_alignStart="@+id/contact_button2"
        android:layout_alignRight="@+id/contact_button2"
        android:layout_alignEnd="@+id/contact_button2"
        android:background="@android:drawable/dialog_holo_light_frame" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 4"
        android:id="@+id/contact_button4"
        android:onClick="onClickbt4"
        android:layout_below="@+id/contact_button3"
        android:layout_alignLeft="@+id/contact_button3"
        android:layout_alignStart="@+id/contact_button3"
        android:layout_marginTop="40dp"
        android:layout_alignRight="@+id/contact_button3"
        android:layout_alignEnd="@+id/contact_button3"
        android:background="@android:drawable/dialog_holo_light_frame" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 5"
        android:id="@+id/contact_button5"
        android:onClick="onClickbt5"
        android:layout_below="@+id/contact_button4"
        android:layout_alignLeft="@+id/contact_button4"
        android:layout_alignStart="@+id/contact_button4"
        android:layout_marginTop="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@android:drawable/dialog_holo_light_frame" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CONTACT 6"
        android:id="@+id/contact_button6"
        android:onClick="onClickbt6"
        android:layout_below="@+id/contact_button5"
        android:layout_alignLeft="@+id/contact_button5"
        android:layout_alignStart="@+id/contact_button5"
        android:layout_marginTop="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@android:drawable/dialog_holo_light_frame" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Security"
        android:id="@+id/textView1"
        android:layout_above="@+id/contact_button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Health &amp; Safety"
        android:id="@+id/textView2"
        android:layout_alignBottom="@+id/contact_button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Transport"
        android:id="@+id/textView3"
        android:layout_above="@+id/contact_button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Maintenance"
        android:id="@+id/textView4"
        android:layout_alignBottom="@+id/contact_button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Housekeeping"
        android:id="@+id/textView5"
        android:layout_alignBottom="@+id/contact_button5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="IT"
        android:id="@+id/textView6"
        android:layout_alignBottom="@+id/contact_button6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

崩溃日志清楚地说明了什么是错误的。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
            at com.prashanth.emergencycontacts.Contacts.onCreate(Contacts.java:65)

您正试图在setText()上调用Button,该空为空。它还会告诉你哪一行崩溃了。

Contacts.onCreate(Contacts.java:65)

这意味着,您需要检查第65行。第65行是

bt1.setText("Javaji P");

bt1null。但是您在以下行中找到了bt1

bt1 = (Button) findViewById(R.id.contact_button1);

这意味着bt1布局中不存在activity_contacts.xml,或者ID contact_button1错误。

请尝试了解崩溃日志的工作原理,因为这是一个非常基本的崩溃,您应该知道如何快速修复它。

答案 1 :(得分:0)

在布局文件中,您指的是带有@ + id的现有元素ID,例如:

机器人:layout_alignLeft =&#34; @ + ID / contact_button1&#34;

创建新ID时应使用@ + id。在引用已创建的id时使用@id,例如:

机器人:layout_alignLeft =&#34; @ ID / contact_button1&#34;