Java 我用这段代码得到了nullpointerexception错误

Java 我用这段代码得到了nullpointerexception错误,java,android,Java,Android,这是我的MainActivity.java文件 public class MainActivity extends Activity implements OnItemSelectedListener{ private Spinner spinner; protected static final String[]paths = {".com", ".org", ".net"}; protected String name,dom,stat; private boolean softInput

这是我的MainActivity.java文件

public class MainActivity extends Activity implements OnItemSelectedListener{

private Spinner spinner;
protected static final String[]paths = {".com", ".org", ".net"};
protected String name,dom,stat;
private boolean softInputActive;
final EditText edit =  (EditText) findViewById(R.id.editText1);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String>adapter = new ArrayAdapter <String(this,android.R.layout.simple_spinner_item,paths);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    InputMethodManager IMEManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    View focusedView = getCurrentFocus();

    // Find the primitive focused view (not ViewGroup)
    while (focusedView instanceof ViewGroup) {
        focusedView = ((ViewGroup) focusedView).getFocusedChild();
    }


    if (hasFocus) {

        if (focusedView instanceof EditText && focusedView.isEnabled() && !IMEManager.isActive(focusedView)) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            softInputActive = true;
        }
    } else if (softInputActive) {
        if (focusedView != null && IMEManager.isActive()) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
        softInputActive = false;
    }

}

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    if(position == 0)
    {
        name = edit.getText().toString();
        dom = paths[0];
    }
    else if (position == 1)
    {
        name = edit.getText().toString();
        dom = paths[1];
    }
    else if(position == 2)
    {
        name = edit.getText().toString();
        dom = paths[2];
    }


    RequestParams params = new RequestParams();
    params.put("src", name);
    params.put("dom", dom);

    NetActivity.post("mhs/", params, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(String response) {

            final RequestParams para = new RequestParams();
                NetActivity.get("mts/default.asp?", para, new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(String Response) {
                        para.put("status",stat);

                    }
            });
        }

        @Override
        public void onFailure(Throwable error, String content) {
            Toast.makeText(getApplicationContext(), "Page Not Found!!",Toast.LENGTH_LONG).show();
        }
    });
}
 public class NetActivity extends Activity{

 private static final String BASE_URL = "http://docs.google.com/";

 private static AsyncHttpClient client = new AsyncHttpClient();

 public static void get(String relativeURL, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      //System.out.println(getAbsoluteUrl(relativeURL));
      client.get(getAbsoluteUrl(relativeURL), params, responseHandler);
      client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  }


 public static void post(String relativeURL, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    //System.out.print(getAbsoluteUrl(relativeURL)) ; 
    client.post(getAbsoluteUrl(relativeURL), params, responseHandler);
    client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
  }


 private static String getAbsoluteUrl(String relativeUrl) {
      return BASE_URL + relativeUrl;
  }
}

这是我的activity_main.xml文件

<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:gravity="clip_horizontal"
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=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="111dp"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="16dp"
    android:ems="10" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/editText1"
    android:layout_toRightOf="@+id/editText1" />
我认为代码中有错误 或者某件事是以错误的方式宣布的 因为当我调试它的时候 它显示nullpointerexception错误 我是新手。 提前谢谢。

也许这是错误的:

final EditText edit =  (EditText) findViewById(R.id.editText1);
您是在膨胀视图之前定义的,因此“编辑”为空。 然后,这里有一个NPE:

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    if(position == 0)
    {
        name = edit.getText().
改变

并将以下行添加到onCreate中:


如果您向我们展示异常堆栈跟踪,这将非常有帮助。请发布所提到的NullPointerException的堆栈跟踪,也许这将有助于问题的完整性,因为在Gabriele的回答中发现的所提供代码中的缺陷无疑是致命的。但是不,堆栈跟踪不是空的-虽然这里不是问题,但更好地使用logcat通常会有所帮助。实际上,可能存在重复的情况,在活动类初始值设定项中根本不能使用UI组件,而只能在创建期间和之后,以及在设置内容视图之后使用。谢谢工作得很好
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    if(position == 0)
    {
        name = edit.getText().
final EditText edit =  (EditText) findViewById(R.id.editText1);
EditText edit;
edit =  (EditText) findViewById(R.id.editText1);