使用listView的android活动可以工作,但不能在调试模式下工作

时间:2014-06-29 15:35:36

标签: android android-layout debugging listview usb

我知道这看起来很奇怪,但我有一个Activity不能在usb调试模式下工作,而是当我断开USB电缆时它工作。有人知道原因吗? 我发布以下活动:

 package com.wikibuyers.aforismi;

 import java.util.ArrayList;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.AdapterView.OnItemClickListener;

 public class PreferitiActivity extends Activity{

public final static String EXTRA_MESSAGE =      "com.example.provacomunicazione.MESSAGE";
private static final int DIALOG_ALERT_ID = 1;
SharedPreferences savedPreferiti;
Intent sendIntent = new Intent();
ListView listView = null;

@Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.preferiti);

      savedPreferiti=getSharedPreferences("preferiti", MODE_PRIVATE);
      String[] listaFrasi = savedPreferiti.getAll().keySet().toArray(new    String[0]);




       Button reset = (Button)findViewById(R.id.button1);



      reset.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {reset();}
      });



      ArrayList<String> Frasi = new ArrayList<String>();

      for (int i=0;i<listaFrasi.length;++i){


          Frasi.add(savedPreferiti.getString("Frase"+i, "Frase"+i));
      }







      ArrayAdapter<String> adapter = new ArrayAdapter<String>  (getApplicationContext(), R.layout.list_item_2, Frasi);

     listView = (ListView) findViewById(R.id.listView3);

      listView.setAdapter(adapter);

      listView.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) { String frase = (String) (((TextView)view).getText())  ;

                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, frase);
                    sendIntent.setType("text/plain");


                    showDialog(DIALOG_ALERT_ID);
            }
        });




   }



@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch (id) {

    case DIALOG_ALERT_ID:
        dialog = createAlertDialog();
        break;
    default:
        dialog = null;
        break;
    }
    return dialog;
}




private Dialog createAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Attenzione");
    builder.setMessage("Solo per Facebook bisogna incollare il testo sulla  casella di condivisione tenendo premuto a lungo e cliccando sul popup ''incolla'' che  compare");
    builder.setCancelable(false);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivity(Intent.createChooser(sendIntent, "Condividi  la frase con:"));
            dismissDialog(DIALOG_ALERT_ID);

        }
    });

    AlertDialog alert = builder.create();
    return alert;

  }



  public void reset(){



         String[] listaFrasi = savedPreferiti.getAll().keySet().toArray(new String[0]);


                if (listaFrasi.length>=1){

                    SharedPreferences.Editor preferencesEditor = savedPreferiti.edit();
                preferencesEditor.remove("Frase"+(listaFrasi.length-1));   //cancella l'ultimo preferito della lista

                 preferencesEditor.commit();

                 Intent intent = getIntent();
                 finish();
                 startActivity(intent); 
                }
}

}

这是由Activity激活的布局preferiti.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/src/com.wikibuyers.aforismi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:keepScreenOn="true">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/preferiti"
    android:textColor="#DDF3FF"
    android:textStyle="bold|italic"
    android:gravity="center"
    android:textSize="36sp" />

<ListView
    android:id="@+id/listView3"
    android:layout_width="match_parent"
    android:layout_height="450dp" >


</ListView>

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="50dp"
     android:textColor="#000000"
    android:text="Elimina l'ultima frase"
    android:layout_gravity="right"
    android:background="@drawable/angolismussatibackground" />

</LinearLayout>

最后列出名为list_item_2.xml的项目:

 <?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/src/com.wikibuyers.aforismi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textColor="#DDF3FF"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    style="@style/menuTextViewStyle.Corsivo"
    android:background="@drawable/angolismussatibackgroundgradiente" 
    android:padding="20dp"
/>

1 个答案:

答案 0 :(得分:0)

正如joao2fast4u所建议的,这是我已经重新报道的图书馆的问题。