asynctask を使用してサーバーにテキストを送信する Android アプリを作成しようとしました。1 つのボタンを正常に実行してテストしましたが、現在、5 つのボタンと 5 つのテキストビューで問題が発生しています。
私の 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);
}
}
非同期タスク コード:
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();
}
}
}
これは私のログキャットが言うことです:
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