-1

this is my MainActivity.java file

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();
        }
    });
}

}

and this is my NetActivity.java file

 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;
  }

}

and this is my activity_main.xml file

<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" />

LOGCAT Details:

06-09 16:42:45.557: E/Trace(917): error opening trace file: No such file or directory (2)
06-09 16:42:45.657: W/ActivityThread(917): Application com.example.mtsdomain is waiting for the debugger on port 8100...
06-09 16:42:45.707: I/System.out(917): Sending WAIT chunk
06-09 16:42:45.747: I/dalvikvm(917): Debugger is active
06-09 16:42:45.917: I/System.out(917): Debugger has connected
06-09 16:42:45.917: I/System.out(917): waiting for debugger to settle...
06-09 16:42:46.127: I/System.out(917): waiting for debugger to settle...
06-09 16:42:46.327: I/System.out(917): waiting for debugger to settle...
06-09 16:42:46.527: I/System.out(917): waiting for debugger to settle...
06-09 16:42:46.793: I/System.out(917): waiting for debugger to settle...
06-09 16:42:46.987: I/System.out(917): waiting for debugger to settle...
06-09 16:42:47.197: I/System.out(917): waiting for debugger to settle...
06-09 16:42:47.399: I/System.out(917): waiting for debugger to settle...
06-09 16:42:47.600: I/System.out(917): waiting for debugger to settle...
06-09 16:42:47.831: I/System.out(917): debugger has settled (1517)

i think there is some mistake in the code or may b something is declared in wrong way because when i debug it it shows nullpointerexception error i m new to it. thanks in advance.

Shubham
  • 165
  • 2
  • 11
  • 1
    It'd be very helpful if you show us the Exception stack trace – Matias Cicero Jun 09 '14 at 17:28
  • Please post the StackTrace for the mentioned NullPointerException – Martin Seeler Jun 09 '14 at 17:30
  • Maybe this will help http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – Pshemo Jun 09 '14 at 17:32
  • The question is complete enough to answer, because the flaw **in the provided code** which has been identified in Gabriele's answer is unquestionably fatal. But no, the stack trace is not null - **while not an issue here**, better skills with logcat would be a help in general. – Chris Stratton Jun 09 '14 at 17:35
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Jun 09 '14 at 17:37

2 Answers2

2

Maybe this is wrong:

final EditText edit =  (EditText) findViewById(R.id.editText1);

You are defining this before inflating your view, so edit is null. Then you have a NPE here:

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    if(position == 0)
    {
        name = edit.getText().
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Indeed, one cannot use UI components at all in the activity class initializer, but only during and after onCreate() - and *after* setting a content view. – Chris Stratton Jun 09 '14 at 17:33
0

Change

final EditText edit =  (EditText) findViewById(R.id.editText1);

to

EditText edit;

and add the following line to your onCreate():

edit =  (EditText) findViewById(R.id.editText1);
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268