这是我的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();
}
});
}
}
这是我的NetActivity.java文件
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" />
LOGCAT详细信息:
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)
我认为代码中存在一些错误 或者可能以错误的方式宣布某事 因为当我调试它 它显示nullpointerexception错误 我是新手。 提前谢谢。
答案 0 :(得分:2)
也许这是错的:
final EditText edit = (EditText) findViewById(R.id.editText1);
您在对视图进行夸大之前定义了这一点,因此edit
为null
。
那你在这里有一个NPE:
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if(position == 0)
{
name = edit.getText().
答案 1 :(得分:0)
更改
final EditText edit = (EditText) findViewById(R.id.editText1);
到
EditText edit;
并将以下行添加到onCreate()
:
edit = (EditText) findViewById(R.id.editText1);