好的,我试图让我的应用程序做的是以下内容。
要了解的重要事实:
这是我当前的onCreate方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
setGPS();
context = this;
Button startButton = (Button) findViewById(R.id.startbutton);
Button stopButton = (Button) findViewById(R.id.stopbutton);
Log.d("NULL POINTER", "NULL POINTER");
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("NULL POINTER", "NULL POINTER");
//setContentView(R.layout.stop);
startService();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//setContentView(R.layout.results);
stopService();
}
});
好的,在此代码中,您将看到两个Log.d打印件,上面写着“NULL POINTER”。当我运行我的代码时,第一个显示但第二个不显示,显然意味着中间的代码导致程序崩溃,这会显示在日志中:
06-15 12:30:25.176: DEBUG/AndroidRuntime(1037): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-15 12:30:25.185: DEBUG/AndroidRuntime(1037): CheckJNI is ON
06-15 12:30:25.537: DEBUG/AndroidRuntime(1037): --- registering native functions ---
06-15 12:30:25.806: DEBUG/ddm-heap(1037): Got feature list request
06-15 12:30:26.147: INFO/ActivityManager(52): Starting activity: Intent {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=Hartford.gps/.GPSMain }
06-15 12:30:26.177: INFO/ActivityManager(52): Start proc Hartford.gps for activity Hartford.gps/.GPSMain: pid=1043 uid=10030 gids={}
06-15 12:30:26.196: DEBUG/AndroidRuntime(1037): Shutting down VM
06-15 12:30:26.196: DEBUG/dalvikvm(1037): DestroyJavaVM waiting for non-daemon threads to exit
06-15 12:30:26.196: DEBUG/dalvikvm(1037): DestroyJavaVM shutting VM down
06-15 12:30:26.196: DEBUG/dalvikvm(1037): HeapWorker thread shutting down
06-15 12:30:26.207: DEBUG/dalvikvm(1037): HeapWorker thread has shut down
06-15 12:30:26.207: DEBUG/jdwp(1037): JDWP shutting down net...
06-15 12:30:26.226: INFO/dalvikvm(1037): Debugger has detached; object registry had 1 entries
06-15 12:30:26.226: DEBUG/dalvikvm(1037): VM cleaning up
06-15 12:30:26.246: ERROR/AndroidRuntime(1037): ERROR: thread attach failed
06-15 12:30:26.317: DEBUG/dalvikvm(1037): LinearAlloc 0x0 used 638596 of 5242880 (12%)
06-15 12:30:26.456: DEBUG/ddm-heap(1043): Got feature list request
06-15 12:30:26.906: DEBUG/NULL POINTER(1043): NULL POINTER
06-15 12:30:26.916: DEBUG/AndroidRuntime(1043): Shutting down VM
06-15 12:30:26.916: WARN/dalvikvm(1043): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-15 12:30:26.916: ERROR/AndroidRuntime(1043): Uncaught handler: thread main exiting due to uncaught exception
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): java.lang.RuntimeException: Unable to start activity ComponentInfo{Hartford.gps/Hartford.gps.GPSMain}: java.lang.NullPointerException
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.os.Looper.loop(Looper.java:123)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at java.lang.reflect.Method.invoke(Method.java:521)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at dalvik.system.NativeStart.main(Native Method)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): Caused by: java.lang.NullPointerException
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at Hartford.gps.GPSMain.onCreate(GPSMain.java:70)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-15 12:30:26.926: ERROR/AndroidRuntime(1043): ... 11 more
我在我的智慧结束时试图让它发挥作用!我已经尝试了所有我能想到的东西,但我认为在这个阶段我有视觉,只需要另一组眼睛来看它。
如果需要,我可以发布更多代码
任何人都可以提供任何帮助都很棒!
@ Mark
哦,好的,谢谢!你是怎么说他们可能是空的?这是start.xml文件,它与stop.xml 相同<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/startbutton"
android:layout_width="100px"
android:layout_height="100px"
android:text="Start"
android:layout_centerInParent="true"
/>
</LinearLayout>
@ Egor和Mah
这里是标有
的第70行的完整文件import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GPSMain extends Activity {
PowerManager powerManager;
PowerManager.WakeLock wL;
//text views to display latitude and longitude
static TextView latituteField;
static TextView longitudeField;
static TextView kmphSpeedField;
static TextView avgKmphField;
static TextView topKmphField;
static TextView accText;
//objects to store positional information
protected static double lat;
protected static double lon;
//objects to store values for current and average speed
protected static double kmphSpeed;
protected static double avgKmph;
protected static double totalKmph;
protected static double topKmph=0;
static String a, b, c;
private Context context;
//counter that is incremented every time a new position is received, used to calculate average speed
static int counter = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
setGPS();
context = this;
Button startButton = (Button) findViewById(R.id.startbutton);
Button stopButton = (Button) findViewById(R.id.stopbutton);
Log.d("NULL POINTER", "NULL POINTER");
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("NULL POINTER", "NULL POINTER");
//setContentView(R.layout.stop);
startService();
}
});
70 stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//setContentView(R.layout.results);
stopService();
}
});
latituteField = (TextView) findViewById(R.id.lat);
longitudeField = (TextView) findViewById(R.id.lon);
kmphSpeedField = (TextView) findViewById(R.id.kmph);
avgKmphField = (TextView) findViewById(R.id.avgkmph);
topKmphField = (TextView) findViewById(R.id.topkmph);
accText = (TextView) findViewById(R.id.acctext);
}
void setGPS(){
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wL = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"My Tag");
}
static String roundTwoDecimalFloat(float a){
String formattedNum;
NumberFormat nf = new DecimalFormat();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
formattedNum = nf.format(a);
return formattedNum;
}
void startService(){
wL.acquire();
Log.d("DEBUG", "start");
startService(new Intent(this, AccelerometerReader.class));
Toast.makeText(context, "Accessing Accelerometer", Toast.LENGTH_LONG).show();
//startService(new Intent(this, Calculations.class));
//Toast.makeText(context, "Acquiring GPS Locations", Toast.LENGTH_LONG).show();
}
void stopService(){
wL.release();
Calculations.locationManager.removeUpdates(Calculations.locationListener);
stopService(new Intent(this, AccelerometerReader.class));
Toast.makeText(context, "Terminating Accelerometer", Toast.LENGTH_LONG).show();
//stopService(new Intent(this, Calculations.class));
//Toast.makeText(context, "Terminating Connection", Toast.LENGTH_LONG).show();
}
static void update(){
latituteField.setText("Current Latitude: "+String.valueOf(lat));
longitudeField.setText("Current Longitude: "+String.valueOf(lon));
kmphSpeedField.setText("Cuttent Speed (kmph): "+String.valueOf(kmphSpeed));
avgKmphField.setText("Average Speed (kmph): "+String.valueOf(avgKmph));
topKmphField.setText("Top Speed (kmph): "+String.valueOf(topKmph));
accText.setText("Accelerometer Values: "+" x: " + a + " y: " + b + " z: " + c);
}
}
答案 0 :(得分:1)
你断言两个Log语句证明它们之间发生了错误。 onClick()代码将在稍后执行。
导致错误的最可能原因是startButton
或stopButton
为空,并且您的findViewById
次查找中的一个或两个都失败了。尝试在日志语句中转储它们,或者在调试器下运行它们并检查它们以查看哪个为空。
Button stopButton = (Button) findViewById(R.id.stopbutton);
将stopButton
设置为null,因为它在您的布局中不存在。然后尝试将OnClickListener添加到此null对象,这会导致您的NPE。