我尝试制作一个Android应用程序,使用asynctask将文本发送到服务器。我成功运行并测试了一个按钮,现在我遇到了五个按钮和五个文本视图的问题。
我的activity_main布局:
<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:background="@drawable/bckgrnd_2"
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" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/btn1"
android:onClick="buttonClicked1" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="buttonClicked2"
android:text="@string/btn2" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="@string/btn3"
android:onClick="buttonClicked3" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="@string/btn4"
android:onClick="buttonClicked4" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/button4"
android:onClick="buttonClicked5"
android:text="@string/btn5" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:text="play1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText2"
android:text="play2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText3"
android:text="play3" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText4"
android:text="play4" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText5"
android:text="play5" />
</RelativeLayout>
我的主要活动代码:
public class MainActivity extends Activity {
private EditText textField;
private EditText textField1;
private EditText textField2;
private EditText textField3;
private EditText textField4;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.editText1);
textField1 = (EditText) findViewById(R.id.editText2);
textField2 = (EditText) findViewById(R.id.editText3);
textField3 = (EditText) findViewById(R.id.editText4);
textField4 = (EditText) findViewById(R.id.editText5);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
textField.setVisibility(View.GONE);
textField1.setVisibility(View.GONE);
textField2.setVisibility(View.GONE);
textField3.setVisibility(View.GONE);
textField4.setVisibility(View.GONE);
}
public void buttonClicked1(View v){
Toast.makeText(this, "Song Has Been Successfully Added!", Toast.LENGTH_SHORT).show();
new asynctask().execute(textField);
}
public void buttonClicked2(View v) {
Toast.makeText(this, "Song Has Been Successfully Added!", Toast.LENGTH_SHORT).show();
new asynctask().execute(textField1);
}
public void buttonClicked3(View v) {
Toast.makeText(this, "Song Has Been Successfully Added!", Toast.LENGTH_SHORT).show();
new asynctask().execute(textField2);
}
public void buttonClicked4(View v) {
Toast.makeText(this, "Song Has Been Successfully Added!", Toast.LENGTH_SHORT).show();
new asynctask().execute(textField3);
}
public void buttonClicked5(View v) {
Toast.makeText(this, "Song Has Been Successfully Added!", Toast.LENGTH_SHORT).show();
new asynctask().execute(textField4);
}
}
Asynctask代码:
public class asynctask extends AsyncTask<View, Integer, Socket>
{
private static final String IP_ADDRESS = "192.168.1.106";
private static final int DEST_PORT = 7777;
private EditText mTextField;
protected Socket doInBackground(View... params)
{
if (params.length != 1)
throw new IllegalArgumentException();
mTextField = (EditText) params[0];
Socket client = null;
try
{
client = new Socket(IP_ADDRESS, DEST_PORT);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return client;
}
protected void onPostExecute(Socket client)
{
try
{
PrintWriter printwriter;
String messsage;
messsage = mTextField.getText().toString();
mTextField.setText("");
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage);
printwriter.flush();
printwriter.close();
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
这就是我的logcat所说的:
09-12 01:28:30.914: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-12 01:28:30.914: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-12 01:28:30.914: E/AndroidRuntime(560): at dalvik.system.NativeStart.main(Native Method)
09-12 01:28:30.914: E/AndroidRuntime(560): Caused by: java.lang.NullPointerException
09-12 01:28:30.914: E/AndroidRuntime(560): at com.timskie.dancingfountainv1.MainActivity.onCreate(MainActivity.java:46)
09-12 01:28:30.914: E/AndroidRuntime(560): at android.app.Activity.performCreate(Activity.java:4465)
09-12 01:28:30.914: E/AndroidRuntime(560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-12 01:28:30.914: E/AndroidRuntime(560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-12 01:28:30.914: E/AndroidRuntime(560): ... 11 more
答案 0 :(得分:0)
ID不正确:
textField = (EditText) findViewById(R.id.editText1); // ???
textField1 = (EditText) findViewById(R.id.textView1); // Here is text, but above is edit??
textField2 = (EditText) findViewById(R.id.textView2);
textField3 = (EditText) findViewById(R.id.textView3);
textField4 = (EditText) findViewById(R.id.textView4);
答案 1 :(得分:0)
在您的Java代码中,您的ID不正确:
textField1 = (EditText) findViewById(R.id.editText2);
textField2 = (EditText) findViewById(R.id.editText3);
textField3 = (EditText) findViewById(R.id.editText4);
textField4 = (EditText) findViewById(R.id.editText5);
在XML中,它们是:
`android:id="@+id/textView2'
依旧......
此外,在XML中,它们是TextView
,在Java中,它们是`EditText'。将Java中的ID更改为:
textField1 = (EditText) findViewById(R.id.textView1);
textField2 = (EditText) findViewById(R.id.textView2);
textField3 = (EditText) findViewById(R.id.textView3);
textField4 = (EditText) findViewById(R.id.textView4);
然后在Java中将它们全部更改为TextView
,或者将它们全部更改为EditText
。我假设你想要TextView
。
初始化View
时,您在
findViewById(R.id.someId) // This should be an id in your XML
应对应于您在XML文件中创建的id
。由于它们是null
,因此当您在此处调用方法时,您会获得NPE
:
textField1.setVisibility(View.GONE);