2 charmingwong CharmingWong 于 2016.09.07 19:11 提问

启动Activity出现NullPointerException异常

package com.charming.sensor;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

private TextView valueView;
private View top, left, right, bottom;
private SensorManager sensorManager;
private Sensor accelerometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

private void init() {
    valueView = (TextView) findViewById(R.id.values);
    top = findViewById(R.id.top);
    bottom = findViewById(R.id.bottom);
    left = findViewById(R.id.left);
    right = findViewById(R.id.right);
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    float[] values = event.values;
    float x = values[0] / 10;
    float y = values[1] / 10;
    int scaleFactor;
    if (x > 0) {
        scaleFactor = (int) Math.min(x * 255, 255);
        right.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        left.setBackgroundColor(Color.TRANSPARENT);
    } else {
        scaleFactor = (int) Math.min(x * 255, 255);
        left.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        right.setBackgroundColor(Color.TRANSPARENT);
    }

    if (y > 0) {
        scaleFactor = (int) Math.min(x * 255, 255);
        top.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        bottom.setBackgroundColor(Color.TRANSPARENT);
    } else {
        scaleFactor = (int) Math.min(x * 255, 255);
        bottom.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        top.setBackgroundColor(Color.TRANSPARENT);
    }

    valueView.setText("x:" + x + " " + "y:" + y);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

布局文件
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.charming.sensor.MainActivity">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0, 1, 2">

    <TableRow android:layout_weight="1">

        <view
            android:id="@+id/top"
            android:layout_column="1" />
    </TableRow>

    <TableRow android:layout_weight="1">

        <view
            android:id="@+id/left"
            android:layout_column="0" />

        <view
            android:id="@+id/right"
            android:layout_column="2" />
    </TableRow>

    <TableRow android:layout_weight="1">

        <view
            android:id="@+id/bottom"
            android:layout_column="1" />
    </TableRow>

</TableLayout>

<TextView
    android:id="@+id/values"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.charming.myapplication, PID: 7837
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.charming.myapplication/com.charming.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #16: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #16: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
at android.app.Activity.setContentView(Activity.java:2166)
at com.charming.myapplication.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
at android.app.Activity.setContentView(Activity.java:2166) 
at com.charming.myapplication.MainActivity.onCreate(MainActivity.java:24) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3个回答

SnowWitch
SnowWitch   2016.09.08 09:14

图片说明
这里面的view不是小写开头,是大写的,就像下图图片说明这样子就不会报错了

devmiao
devmiao   Ds   Rxr 2016.09.07 23:02
qq_28026283
qq_28026283   2016.09.08 10:08

view换成View

Csdn user default icon
上传中...
上传图片
插入图片