现在我有两个活动,activity_bustracking和activity_main
当按下主活动中的按钮时,它应该移动到胸围活动;但是,现在当我点击按钮时,应用程序崩溃并出现错误:
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)
我的MainActivity.class看起来像:
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");
}
}
我的BusTrackingActivity.class看起来像:
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);
}
}
我的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>
我的Android清单看起来像:
<?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>
答案 0 :(得分:4)
问题是您从未将context
分配给this
。
但是,没有理由在活动中保存上下文,只需使用MainActivity.this
代替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);
}
答案 1 :(得分:2)
当您在此行中开始新意图时:
Intent intent = new Intent(context, BusTrackingActivity.class);
尚未启动上下文对象。你之前宣布过:
protected Context context;
在onCreate()方法中,使用以下代码:
context = this;
答案 2 :(得分:0)
上下文为null。初始化它或使用MainActivity.this代替上下文。
答案 3 :(得分:0)
添加MainActivity中第67行的代码行,因为这是错误行。
但是如果没有这个,我猜测按钮监听器出了问题。
这就是说,试着删除所有这些:
public void addListenerOnButton() {
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, BusTrackingActivity.class);
startActivity(intent);
}
});
}
听众最好使用他们使用的方法,或者在创建时使用。这可以通过这行代码来完成:
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, BusTrackingActivity.class);
startActivity(intent);
}
});
但实际上,如果这不起作用,我只需要在错误中喜欢代码。如果你点击logcat中的第一个蓝色东西(其余的将是灰色的),我可以提供更多帮助。
答案 4 :(得分:0)
这可能是因为在第一个 Activity 中使用卡片视图或应用在运行时崩溃的那个 Activity。
使用此代码在依赖项中实现卡片视图
implementation 'androidx.cardview:cardview:1.0.0'