2013-09-11 3 views
-1

Я попытался сделать приложение для Android, которое отправит текст на сервер с помощью asynctask. Я успешно побежал и протестировал одну кнопку, и теперь у меня проблемы с пятью кнопками и пятью текстовыми изображениями.Приложение для Android не запускается

Мой макет 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 
+4

вывесить стека пожалуйста – Raghunandan

+0

я всегда получаю сообщ когда я запустить его «К сожалению, приложение перестало работать» – Tim

+2

Post Ваш стек след. – DJhon

ответ

0

В коде Java ваши идентификаторы неверны:

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 s, а на Java они являются `E ditText годов. Измените идентификаторы в Java, чтобы быть:

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 или изменить их в XML для EditText. Я предполагаю, что вы хотите TextView.

При инициализации View, идентификатор ввода в

findViewById(R.id.someId) // This should be an id in your XML 

должны соответствовать id, созданной в файле XML. Так как они null, вы получаете NPE при вызове метода на них, как здесь:

textField1.setVisibility(View.GONE); 
0

идентификаторы не являются правильными:

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); 
+0

О, geez tnx tnx Позвольте мне запустить его снова :) – Tim

+0

Его все тот же T_T приложение не запускается, когда я запускаю его в эмуляторе – Tim

+0

@ user2769688 см. Мою ссылку выше о поиске logcat. Есть слишком много возможностей, не видя, где ошибка – codeMagic

Смежные вопросы