我对发生的事情感到有些困惑......
当EditText id = username时,似乎应用程序运行良好。
但是当我更改了图片中的ID时,应用会停止并产生下面的错误。
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blurred">
<include
android:id="@+id/toolbarSignup"
layout="@layout/toolbar_layout" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/userPic"
android:src="@drawable/ic_upload"
android:layout_below="@+id/toolbarSignup"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="130dp"
android:layout_height="40dp"
android:text="Upload Image"
android:id="@+id/upload"
android:layout_below="@+id/userPic"
android:layout_centerHorizontal="true"
android:background="#03A9F4"
android:alpha="0.50"/>
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:id="@+id/firstname"
android:layout_below="@+id/upload"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="First Name" />
我的logcat:
02-17 07:05:42.756 17367-17367/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: glorioso.david.metrohomes, PID: 17367
java.lang.RuntimeException: Unable to start activity ComponentInfo{glorioso.david.metrohomes/glorioso.david.metrohomes.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at glorioso.david.metrohomes.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
我的活动:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class Signup extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signingup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarSignup);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Registration");
}
}
答案 0 :(得分:0)
您可能忘记更改onCreate
方法中的ID。
将findViewById(R.id.username);
更改为findViewById(R.id.firstname);
希望这能解决你的问题。
答案 1 :(得分:0)
如果您想使用id/firstname
,则在您的活动onCreate method
中应定义Edit Text name=(EditText)findViewById(R.id.firstname)
。
注意到xml中的ID应始终与您的活动相同。