Android Studio:无法创建新活动

时间:2016-03-25 21:01:15

标签: android android-studio

当我尝试通过单击主活动中的按钮启动新活动时,我收到一个nullpointer异常。我确实在manifest.xml中包含了新活动。

这是错误:

 E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.toke.whistapp1/com.example.toke.whistapp1.EnterNames}: java.lang.NullPointerException
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
     at android.app.ActivityThread.access$600(ActivityThread.java:141)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5041)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:511)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
     at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.NullPointerException
     at android.app.Activity.findViewById(Activity.java:1839)
     at com.example.toke.whistapp1.EnterNames.<init>(EnterNames.java:15)
     at java.lang.Class.newInstanceImpl(Native Method)
     at java.lang.Class.newInstance(Class.java:1319)
     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5041) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     at dalvik.system.NativeStart.main(Native Method) 

UDDATE onCreate code:

package com.example.toke.whistapp1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class EnterNames extends AppCompatActivity {

private Button submitButt;
private EditText play1 = (EditText) findViewById(R.id.play1);
private EditText play2 = (EditText) findViewById(R.id.play2);
private EditText play3 = (EditText) findViewById(R.id.play3);
private EditText play4 = (EditText) findViewById(R.id.play4);
private String[] names = new String[4];

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

}

新活动的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.example.toke.whistapp1.EnterNames">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Enter Player Names"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="35dp"
    android:textStyle="bold" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/play1"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textSize="30dp"
    android:hint="Player 1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/play2"
    android:hint="Player 2"
    android:textSize="30dp"
    android:layout_below="@+id/play1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/play3"
    android:hint="Player 3"
    android:textSize="30dp"
    android:layout_below="@+id/play2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/play4"
    android:hint="Player 4"
    android:textSize="30dp"
    android:layout_below="@+id/play3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/subButt"
    android:layout_below="@+id/play4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

在正确初始化内容后,setContentView(...)中调用onCreate(...)之前,您无法查看视图。

将您的代码更改为仅声明视图:

private EditText play1;
private EditText play2;
private EditText play3;
private EditText play4;

然后在onCreate()中分配值:

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

    play1 = (EditText) findViewById(R.id.play1);
    play2 = (EditText) findViewById(R.id.play2);
    play3 = (EditText) findViewById(R.id.play3);
    play4 = (EditText) findViewById(R.id.play4);
}