2016-11-26 92 views
-3

i.m new在android中。 它的應用程序,以方向的朝拜。實際上我有3類:主,GPS服務和RoseGPS(旋轉針)。從那個主要活動類我想調用其他類的功能。當我添加RoseGPS時出現錯誤。幫助我解決它。如何解決無法啓動活動ComponentInfo {.....}:java.lang.NullPointerException

MainActivity

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){ 

} 

RoseGPS

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_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.gpsaja.GPSMainActivity" > 

<TextView 
    android:id="@+id/tvAddress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" 
    android:layout_gravity="center" 
    android:textStyle="bold"/> 

<RelativeLayout 
    android:id="@+id/cantainer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="260dp" 
    android:background="#ffffff" 
    android:layout_gravity="center"> 
    </RelativeLayout> 

<TextView 
    android:id="@+id/tvDegree" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/idDegree" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    > 

    <TextView 
     android:id="@+id/tvLat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/idLat" 
     android:gravity="center"/> 

    <TextView 
     android:id="@+id/tvLong" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/idLon" /> 
</LinearLayout> 

我logcat的

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 ComponentInfo{com.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 
+3

顯然你不能這樣做'新的GPSMainActivity()' – Selvin

回答

0
rose = new RoseGPS(context); 

contextnull。從您的代碼中刪除context字段。

rose = new RoseGPS(this); 

Activity繼承Context,你需要使用活動爲Context反正活動創建部件:那麼,這條線來改變。

相關問題