I created one dash Board Activity on that I created many buttons from that buttons first button is working when I clicked in second button I'm getting error like Unfortunately "Tntrains has stopped".
Why is this happening?
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>
I am getting error in 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)