Reputation: 1623
Right now I have two activities, activity_bustracking and activity_main
When the button in the main activity is pressed it is supposed to move to the bustracking activity; however, right now it when I click the button the application crashes with the error:
05-28 16:34:32.680 26130-26130/com.example.robertloggia.test1 D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
05-28 16:34:32.770 26130-26130/com.example.robertloggia.test1 D/AndroidRuntime﹕ Shutting down VM
05-28 16:34:32.770 26130-26130/com.example.robertloggia.test1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.robertloggia.test1, PID: 26130
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4562)
at com.example.robertloggia.test1.MainActivity$1.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:5162)
at android.view.View$PerformClick.run(View.java:20873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
My MainActivity.class looks like:
package com.example.robertloggia.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener{
//GPS Specific variables
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
boolean gps_enabled, network_enabled;
Spinner routeChoice;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
routeChoice = (Spinner) findViewById(R.id.route_choices);
btnSubmit = (Button) findViewById(R.id.btn_submit);
txtLat = (TextView) findViewById(R.id.textView1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//get location and make sure it is enabled
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(MainActivity.this, "Please enable GPS", Toast.LENGTH_LONG).show();
}
addListenerOnButton();
}
public void addListenerOnButton() {
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(MainActivity.this,
// "OnClickListener : " +
// "\nSpinner 1 : " + String.valueOf(routeChoice.getSelectedItem()),
// Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, BusTrackingActivity.class);
startActivity(intent);
}
});
}
/*
* METHODS FOR GETTING LOCATION
*/
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textView1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude", "disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude", "enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude", "status");
}
}
My BusTrackingActivity.class looks like:
package com.example.robertloggia.test1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
/**
* Created by RobertLoggia on 5/28/15.
*/
public class BusTrackingActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bustracking);
}
}
My activity_main.xml:
<ImageView
android:layout_width="330dp"
android:layout_height="200.5dp"
android:layout_marginTop="110dp"
android:id="@+id/imageView"
android:src="@drawable/ripta_logo"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/logo" />
<Spinner
android:layout_width="290dp"
android:layout_height="50dp"
android:id="@+id/route_choices"
android:entries="@array/route_array"
android:prompt="@string/route_prompt"
android:layout_gravity="center_vertical"
android:layout_below="@+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_text"
android:id="@+id/btn_submit"
android:layout_gravity="center_vertical"
android:layout_alignBottom="@+id/route_choices"
android:layout_toRightOf="@id/route_choices"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please make sure GPS is enabled"
android:id="@+id/textView1"
android:layout_below="@+id/route_choices"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
activity_bustracking.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
And my android manifest looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.robertloggia.test1" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".BusTrackingActivity" >
</activity>
</application>
</manifest>
Upvotes: 0
Views: 1383
Reputation: 27
this could be because of using card view in first activity or that activity on which your app got crashed while running.
used this code to implement card view in dependencies
implementation 'androidx.cardview:cardview:1.0.0'
Upvotes: 0
Reputation: 478
Add the line of code from the 67th line in the MainActivity, as this is the line with the error.
However without this, I am guessing something went wrong with the button listener.
This being said, try removing all of this:
public void addListenerOnButton() {
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, BusTrackingActivity.class);
startActivity(intent);
}
});
}
Listeners are better put in the method they are used, or in this case on create. This can be done by putting this line of code:
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, BusTrackingActivity.class);
startActivity(intent);
}
});
Really though, if this doesn't work I just need to like of code at the error. If you click the first blue thing in the logcat (the rest will be grey), I can help more.
Upvotes: 0
Reputation: 991
context in MainActivity is null.Either initialize it or use MainActivity.this in place of context.
Upvotes: 0
Reputation: 1191
When you are starting the new intent in this line:
Intent intent = new Intent(context, BusTrackingActivity.class);
The context object has not been initiated yet. You declared it earlier:
protected Context context;
Inside your onCreate() method, use this code:
context = this;
Upvotes: 2
Reputation: 43322
The problem is that you never assign context
to this
.
However, there is no reason to save off a Context in an Activity, just use MainActivity.this
instead of context
:
@Override
public void onClick(View v) {
// Toast.makeText(MainActivity.this,
// "OnClickListener : " +
// "\nSpinner 1 : " + String.valueOf(routeChoice.getSelectedItem()),
// Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, BusTrackingActivity.class);
startActivity(intent);
}
Upvotes: 4