如何修复无法启动活动 ComponentInfo.....:java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】如何修复无法启动活动 ComponentInfo.....:java.lang.NullPointerException [重复]【英文标题】:how to fix Unable to start activity ComponentInfo.....: java.lang.NullPointerException [duplicate]如何修复无法启动活动 ComponentInfo.....:java.lang.NullPointerException [重复] 【发布时间】:2017-04-10 15:52:59 【问题描述】:我是安卓新手。 它的应用程序指向朝拜的方向。实际上我有 3 个类:main、GPS servive 和 RoseGPS(旋转针)。从那个主要活动类我想调用其他类的函数。添加 RoseGPS 时出现错误。帮我解决它。
主活动
public class GPSMainActivity extends Activity implements SensorListener
//public class GPSMainActivity extends Activity
TextView textView1,textView2,textView3,textView4;
AppService appService;
Context context;
double latMasjid, lonMasjid;
public static double degree;
private RelativeLayout direcCantainer;
SensorManager sensorManager;
static final int sensor=SensorManager.SENSOR_ORIENTATION;
private RoseGPS rose;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpsmain);
textView1=(TextView) findViewById(R.id.tvAddress);
textView2=(TextView) findViewById(R.id.tvDegree);
textView3=(TextView)findViewById(R.id.tvLat);
textView4=(TextView) findViewById(R.id.tvLong);
appService=new AppService(GPSMainActivity.this);
Location gpsLocation = appService.getLocation();
if (gpsLocation != null)
latMasjid=gpsLocation.getLatitude();
lonMasjid = gpsLocation.getLongitude();
String resultLat = "Latitude: " + gpsLocation.getLatitude() +
" Longitude: " + gpsLocation.getLongitude();
textView4.setText(resultLat);
else
textView3.setText("gagal");
degree=bearing(latMasjid,lonMasjid,21.42243,39.82624);
textView2.setText(String.format("%.2f", degree)+(char)0x00B0);
direcCantainer=(RelativeLayout)findViewById(R.id.cantainer_layout);
rose = new RoseGPS(context);
direcCantainer.addView(rose);
rose.invalidate();
sensorManager=(SensorManager)getSystemService(context.SENSOR_SERVICE);
protected double bearing(double startLat, double startLng, double endLat, double endLng)
double longitude1 = startLng;
double longitude2 = endLng;
double latitude1 = Math.toRadians(startLat);
double latitude2 = Math.toRadians(endLat);
double longDiff= Math.toRadians(longitude2 - longitude1);
double y= Math.sin(longDiff)* Math.cos(latitude2);
double x= Math.cos(latitude1)* Math.sin(latitude2)- Math.sin(latitude1)* Math.cos(latitude2)* Math.cos(longDiff);
return (Math.toDegrees(Math.atan2(y, x))+360)%360;
public void onSensorChanged(int sensor, float[] values)
if(sensor != GPSMainActivity.sensor)
return;
int orientation = (int)values[0];
rose.setDirections(orientation, orientation);
public void onAccuracyChanged(int sensor, int accuracy)
玫瑰GPS
public class RoseGPS extends View
GPSMainActivity fragment1GPSMainActivity = new GPSMainActivity();
private float directionNorth=0;
private float directionQibla=0;
private TextView bearingNorth, bearingQibla;
private String bearingNorthString, bearingQiblaString;
private DecimalFormat df = new DecimalFormat("0.000");
private Bitmap compassBackground, compassNeedle;
private Matrix rotateNeedle=new Matrix();
private int width=240;
private int height=240;
private float centre_x=width*0.5f;
private float centre_y=height*0.5f;
public RoseGPS(Context context)
super(context);
initCompassView();
public RoseGPS(Context context, AttributeSet attrs, int defStyle)
super(context,attrs,defStyle);
initCompassView();
public RoseGPS(Context context, AttributeSet attrs)
super(context, attrs);
initCompassView();
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
setMeasuredDimension(width,height);
private void initCompassView()
compassNeedle=BitmapFactory.decodeResource(getResources(), R.drawable.panah_36);
compassBackground=BitmapFactory.decodeResource(getResources(), R.drawable.kompas_72);
width=compassBackground.getWidth()*2;
height=compassBackground.getHeight()*2;
centre_x=width*0.5f;
centre_y=height*0.5f;
rotateNeedle.postTranslate(centre_x - compassNeedle.getWidth()/2, centre_y - compassNeedle.getHeight()/2);
invalidate();
public void setConstants(TextView bearingNorth, CharSequence bearingNorthString, TextView bearingQibla, CharSequence bearingQiblaString)
this.bearingNorth=bearingNorth;
this.bearingNorthString=bearingNorthString.toString();
this.bearingQibla=bearingQibla;
this.bearingQiblaString=bearingQiblaString.toString();
public void setDirections(float directionsNorth, float directionsQibla)
this.directionNorth=directionsNorth;
this.directionQibla=directionsQibla;
rotateNeedle=new Matrix();
float degree=(float) GPSMainActivity.degree;
rotateNeedle.postRotate(degree, compassNeedle.getWidth()/2, compassNeedle.getHeight()/2);
rotateNeedle.postTranslate(centre_x - compassNeedle.getWidth()/2, centre_y - compassNeedle.getHeight()/2);
invalidate();
protected void onDraw(Canvas canvas)
bearingNorth.setText(bearingNorthString.replace("(1)", df.format(directionNorth)));
bearingQibla.setText(bearingQiblaString.replace("(+/-)", directionQibla>=0 ? "+" : "-").replace("(2)", df.format(Math.abs(directionQibla))).replace("(3)", df.format(directionNorth+directionQibla)));
Paint p=new Paint();
canvas.rotate(-directionNorth, centre_x, centre_y);
canvas.drawBitmap(compassBackground, compassBackground.getWidth()/2, compassBackground.getHeight()/2, p);
canvas.drawBitmap(compassNeedle, rotateNeedle, p);
我的 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
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.gpsaja.GPSMainActivity" >
<TextView
android:id="@+id/tvAddress"
android:layout_
android:layout_
android:text="@string/hello_world"
android:layout_gravity="center"
android:textStyle="bold"/>
<RelativeLayout
android:id="@+id/cantainer_layout"
android:layout_
android:layout_
android:background="#ffffff"
android:layout_gravity="center">
</RelativeLayout>
<TextView
android:id="@+id/tvDegree"
android:layout_
android:layout_
android:layout_gravity="center"
android:text="@string/idDegree"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tvLat"
android:layout_
android:layout_
android:text="@string/idLat"
android:gravity="center"/>
<TextView
android:id="@+id/tvLong"
android:layout_
android:layout_
android:gravity="center"
android:text="@string/idLon" />
</LinearLayout>
我的日志猫
11-24 02:04:51.663: E/AndroidRuntime(2177): FATAL EXCEPTION: main
11-24 02:04:51.663: E/AndroidRuntime(2177): Process: com.gpsaja, PID: 2177
11-24 02:04:51.663: E/AndroidRuntime(2177): java.lang.RuntimeException: Unable to start activity ComponentInfocom.gpsaja/com.gpsaja.GPSMainActivity: java.lang.NullPointerException
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.os.Handler.dispatchMessage(Handler.java:102)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.os.Looper.loop(Looper.java:136)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-24 02:04:51.663: E/AndroidRuntime(2177): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 02:04:51.663: E/AndroidRuntime(2177): at java.lang.reflect.Method.invoke(Method.java:515)
11-24 02:04:51.663: E/AndroidRuntime(2177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-24 02:04:51.663: E/AndroidRuntime(2177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-24 02:04:51.663: E/AndroidRuntime(2177): at dalvik.system.NativeStart.main(Native Method)
11-24 02:04:51.663: E/AndroidRuntime(2177): Caused by: java.lang.NullPointerException
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.view.ViewConfiguration.get(ViewConfiguration.java:352)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.view.View.<init>(View.java:3448)
11-24 02:04:51.663: E/AndroidRuntime(2177): at com.gpsaja.RoseGPS.<init>(RoseGPS.java:33)
11-24 02:04:51.663: E/AndroidRuntime(2177): at com.gpsaja.GPSMainActivity.onCreate(GPSMainActivity.java:64)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.Activity.performCreate(Activity.java:5231)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-24 02:04:51.663: E/AndroidRuntime(2177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-24 02:04:51.663: E/AndroidRuntime(2177): ... 11 more
【问题讨论】:
显然你不能这样做new GPSMainActivity()
【参考方案1】:
rose = new RoseGPS(context);
context
是 null
。从您的代码中删除 context
字段。然后,将此行更改为:
rose = new RoseGPS(this);
由于Activity
继承自Context
,因此您需要为活动创建小部件,并将活动用作Context
。
【讨论】:
以上是关于如何修复无法启动活动 ComponentInfo.....:java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Android 错误:无法启动活动 ComponentInfo..?
无法启动活动 ComponentInfo...add_team:java.lang.NullPointerException [重复]
安装 Android SDK Build-Tools 26 后无法启动活动 ComponentInfo...
无法启动活动 ComponentInfo java.lang.RuntimeException:无法创建 webview