为什么我不幸的是应用已经停止....?

时间:2016-07-05 08:20:43

标签: android nullpointerexception

我创建了一个破折号Board活动,我创建了许多按钮,当我点击第二个按钮时,第一个按钮正在工作我得到错误就像不幸" Tntrains已停止"。

为什么会这样?

dashboard.xml:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.jeshtamsru.tntrains.DashboarsActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="300dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:text="chennai to coimbatore"
                android:id="@+id/chennaitocoimbatore_button"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="300dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:text="chennai to kanyakumari"
                android:id="@+id/chennaikanya_button"/>
        </RelativeLayout>
</LinearLayout>

Dashboard.java:

public class DashboarsActivity extends AppCompatActivity {
    Button chennaitocoi,chennaitokanya;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboars);
        chennaitocoi=(Button)findViewById(R.id.chennaitocoimbatore_button);
        chennaitocoi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {Intent chcointent=new Intent(getApplicationContext(),ChennaitocoimbatorRootActivity.class);
                startActivity(chcointent);
            }
        });
        chennaitokanya=(Button)findViewById(R.id.chennaikanya_button);
        chennaitokanya.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {Intent chekanintent=new        Intent(getApplicationContext(),ChennaikanyaRootActivity.class);
                startActivity(chekanintent);
            }
        });
    }
}

chennaitokanyakumariroot.java:

public class ChennaikanyaRootActivity extends AppCompatActivity {
    String[] source = {"Chennai", "Kanyakumari", "Nagercoil"};
    String[] destination = {"Chennai", "Kanyakumari", "Nagercoil"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chennaikanya_root);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.select_dialog_item, source);
        //Getting the instance of AutoCompleteTextView
        final AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.source_auto);
        actv.setThreshold(1);//will start working from first character
        actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
        actv.setTextColor(Color.BLACK);


        //destination auto complete
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.select_dialog_item, destination);
        //Getting the instance of AutoCompleteTextView
        final AutoCompleteTextView actv2 = (AutoCompleteTextView) findViewById(R.id.destination_auto);
        actv2.setThreshold(1);//will start working from first character
        actv2.setAdapter(arrayAdapter);//setting the adapter data into the AutoCompleteTextView
        actv2.setTextColor(Color.BLACK);
        //Button onclick
        Button button2 = (Button) findViewById(R.id.cksearch_button1);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (actv.getText().toString().equals("Chennai") && actv2.getText().toString().equals("Kanyakumari")){
                    Intent i = new Intent(ChennaikanyaRootActivity.this, ChennaitokanyatraindetailsActivity.class);
                    startActivity(i);

                } else if (actv.getText().toString().equals("Kanyakumari") && actv2.getText().toString().equals("Chennai")) { Intent i = new Intent(ChennaikanyaRootActivity.this, KanyatochennaitraindetailsActivity.class);
                    startActivity(i);
                }
            }
        });
    }
}

chennaikanyakumariroot.xml:

 <LinearLayout 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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jeshtamsru.tntrains.ChennaikanyaRootActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="50sp">
        <AutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Current Location"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:id="@+id/cksource_auto"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50sp">
        <AutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Destination Location"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:id="@+id/ckdestination_auto"
            />

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="     click here to find your train     "
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30sp"
        android:id="@+id/cksearch_button1">
    </Button>

</LinearLayout>

我在log cat中遇到错误:

Caused by: java.lang.NullPointerException: Attempt to
    android.widget.AutoCompleteTextView.setThreshold(int)'
    on a null object reference at
    com.example.jeshtamsru.tntrains
        .ChennaikanyaRootActivity.onCreate(ChennaikanyaRootActivity.java:26)

1 个答案:

答案 0 :(得分:0)

将以下行添加到onCreate():

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
    // if all ok, redirecting
    header("Location: http://example.com/myOtherPage.php");
    die();
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

编辑您的代码,如下所示:

chennaitokanya=(Button)findViewById(R.id.chennaikanya_button);