0

我的一项活动有一个问题,就是当我在 android Ginberbread 2.3 中测试我的应用程序时,一切都很好。但是当我在 Android ICS 或 Jeally bean 中测试我的应用程序时,它会在最后一个活动中强制关闭,这是调试中显示的错误:

    08-11 18:46:55.666: E/AndroidRuntime(16107): FATAL EXCEPTION: main
08-11 18:46:55.666: E/AndroidRuntime(16107): java.lang.RuntimeException: Unable to 
instantiate activity ComponentInfo{com.prueba.bibliotecaappprueba/com.prueba.bibliotecaappprueba.DetallesActivity}: java.lang.NullPointerException
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1973)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.os.Looper.loop(Looper.java:137)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread.main(ActivityThread.java:4697)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at java.lang.reflect.Method.invoke(Method.java:511)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at dalvik.system.NativeStart.main(Native Method)
08-11 18:46:55.666: E/AndroidRuntime(16107): Caused by: java.lang.NullPointerException
08-11 18:46:55.666: E/AndroidRuntime(16107):    at com.prueba.bibliotecaappprueba.FileCache.<init>(FileCache.java:17)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at com.prueba.bibliotecaappprueba.ImageLoader.<init>(ImageLoader.java:33)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at com.prueba.bibliotecaappprueba.DetallesActivity.<init>(DetallesActivity.java:50)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at java.lang.Class.newInstanceImpl(Native Method)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at java.lang.Class.newInstance(Class.java:1319)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-11 18:46:55.666: E/AndroidRuntime(16107):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
08-11 18:46:55.666: E/AndroidRuntime(16107):    ... 11 more
08-11 18:47:02.604: D/dalvikvm(16143): GC_FOR_ALLOC freed 28K, 2% free 12847K/13027K, paused 16ms

我不知道为什么它在 Ginberbread 中有效,但在 ICS 中无效,请帮助我!谢谢。

我一直在寻求其他答案的帮助,但对我没有帮助:/谢谢!


编辑

这是活动代码正在崩溃。

public class DetallesActivity extends Activity {
//Variables
    public static final String PREFS_NAME1 = "MyPrefsFile1";
    public ImageLoader imageLoader;
    {

        imageLoader = new ImageLoader(null);
    }

    TextView txtNama;
    TextView txtLinkImage;
    TextView txtAlamat;
    TextView txtTelepon;
    TextView txtFax;
    TextView txtEmail;
    TextView txtWebsite;
    String descarg;
    String id_rs;
    String nomb;
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String url_detail_rs = "http://lectodrid.p.ht/LibrosPHP/Libros/detalles.php";
    public int i = 0;
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_DAFTAR_RS = "daftar_rs";
    private static final String TAG_ID_RS = "id_rs";
    public static final String TAG_NOMBRE_RS = "nombre_rs";
    public static final String TAG_LINK_IMAGE_RS = "link_image_rs";
    public static final String TAG_AUTOR_RS = "autor_rs";
    public static final String TAG_CATEG_RS = "categ_rs";
    public static final String TAG_LINK_DESCARGA_RS = "link_descarga_rs";;
    private static final String TAG_SIPNOSIS_RS = "sipnosis";
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private ProgressDialog mProgressDialog;
  //Variables fin
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_rs);


        Intent i = getIntent();

        id_rs = i.getStringExtra(TAG_ID_RS);

        new GetDetailrs().execute();
         SharedPreferences settings = getSharedPreferences(PREFS_NAME1, 0);
            boolean dialogShown = settings.getBoolean("dialogShown", false);


            if (!dialogShown) {
              // AlertDialog code here
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("El Botón de Descarga se encuentra al final de la descripción" + "\n" +
                        "- Haz Scroll hasta llegar al final."+ "\n" +
                        "- Pulsa el botón 'Descargar' y espera unos segundos." + "\n" +
                        "Podrás disfrutar de tu Libro descargado desde tu Lector de preferencia!. ")
                        .setTitle("Atencion!")
                        .setCancelable(false)
                        .setNeutralButton("Aceptar",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();
                ((TextView)alert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
              SharedPreferences.Editor editor = settings.edit();
              editor.putBoolean("dialogShown", true);
              editor.commit();    


            }
    }



//Termina Bundle
    private void startDownload() {
        String url = descarg;
        new DownloadFileAsync().execute(url);
    }
//Termina inicio de descarga
    //Termina el inicio de descarga
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Descargando Libro..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
            }
        }
    //Termina el DIalog descargando...
        class DownloadFileAsync extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                showDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
            //termina einicio dialgo
            @Override
            protected String doInBackground(String... aurl) {
                int count;

            try {

            URL url = new URL(aurl[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            int lenghtOfFile = c.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/Mi Biblioteca/"+nomb+ ".epub");

            byte data[] = new byte[1024];

            long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""+(int)((total*100)/lenghtOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {}
            return null;

            }
            //Termina almacenamiento

    protected void onProgressUpdate(String... progress) {
         Log.d("ANDRO_ASYNC",progress[0]);
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));

    }
    //Progreso llega a 0

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
          Intent intent1 = getIntent();
          finish();
          startActivity(intent1);

    }
    //Progreso se cierra
        }
    class GetDetailrs extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DetallesActivity.this);
            pDialog.setMessage("Cargando esperar ... !");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... params) {
            Conexion();
            if (i == 0) {
            runOnUiThread(new Runnable() {
                public void run() {

                    int success;
                    try {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("id_rs",
                                id_rs));

                        JSONObject json = jsonParser.makeHttpRequest(
                                url_detail_rs, "GET", params);

                        Log.d("rs Detail", json.toString());

                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {

                            JSONArray productObj = json
                                    .getJSONArray(TAG_DAFTAR_RS);

                            JSONObject daftar_rs = productObj.getJSONObject(0);

                            txtNama = (TextView) findViewById(R.id.nama_rs);
                            txtLinkImage = (TextView) findViewById(R.id.link_image_rs);
                            txtAlamat = (TextView) findViewById(R.id.alamat_rs);
                            txtTelepon = (TextView) findViewById(R.id.telepon_rs);
                            txtFax = (TextView) findViewById(R.id.fax_rs);
                            txtEmail = (TextView) findViewById(R.id.email_rs);


                            ImageView thumb_image = (ImageView) findViewById(R.id.list_image);

                            txtNama.setText(daftar_rs.getString(TAG_NOMBRE_RS));

                            txtLinkImage.setText(daftar_rs
                                    .getString(TAG_LINK_IMAGE_RS));
                            txtAlamat.setText(daftar_rs
                                    .getString(TAG_AUTOR_RS));
                            txtTelepon.setText(daftar_rs
                                    .getString(TAG_CATEG_RS));
                            txtFax.setText(daftar_rs.getString(TAG_SIPNOSIS_RS));
                            txtEmail
                                    .setText(daftar_rs.getString(TAG_LINK_DESCARGA_RS));

                            descarg = new String(daftar_rs.getString(TAG_LINK_DESCARGA_RS));
                            nomb = new String(daftar_rs.getString(TAG_NOMBRE_RS));

                            imageLoader.DisplayImage(daftar_rs
                                    .getString(TAG_LINK_IMAGE_RS),
                                    thumb_image);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
            }
            else{ finish();
            }
            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();
//comiena prueba

            File extStore = Environment.getExternalStorageDirectory();
            File myFile = new File(extStore.getAbsolutePath() + "/Mi Biblioteca/"+nomb+".epub");
            if(myFile.exists()){

                Button leer = (Button) findViewById(R.id.Btnleer);
                leer.setVisibility(View.VISIBLE);
                Button dscr = (Button) findViewById(R.id.BtnDesc);
                dscr.setVisibility(View.INVISIBLE);
            }else{


                Button leer = (Button) findViewById(R.id.Btnleer);
                leer.setVisibility(View.INVISIBLE);
                Button dscr = (Button) findViewById(R.id.BtnDesc);
                dscr.setVisibility(View.VISIBLE);


            }
            //Termina el If e inicia descarga
            Button dscr = (Button) findViewById(R.id.BtnDesc);
            dscr.setOnClickListener(new OnClickListener(){
                 public void onClick(View v) {
                     startDownload();}

            });

            //inicia abrir archivo
            Button leer = (Button) findViewById(R.id.Btnleer);
            leer.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v2) {
                    // TODO Auto-generated method stub
                    File file = new File("/sdcard/Mi Biblioteca/"+nomb+".epub");
                    Intent intentreadf = new Intent(Intent.ACTION_VIEW);
                    intentreadf.setDataAndType(Uri.fromFile(file),"application/epub+zip");
                    intentreadf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                    try {
                        startActivity(intentreadf);
                    } 
                    catch (ActivityNotFoundException e) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(DetallesActivity.this);
                        builder.setMessage("No hay instalada Aplicacion Para leer Epubs" +
                                "Desea descargar una sugerída por Droidteca?")
                        .setTitle("Advertencia")
                        .setCancelable(false)
                        .setNegativeButton("Cancelar",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();

                                    }
                        })
                        .setPositiveButton("Descargar",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        try {
                                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.zoreader")));
                                        } catch (android.content.ActivityNotFoundException anfe) {
                                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.zoreader")));
                                        }
                                   // metodo que se debe implementar
                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();
    } 
                }

                });
            //finaliza abrir archivo
        }
    }
    private void Conexion() {
        // TODO Auto-generated method stub
        try {
            if (InetAddress.getByName("www.google.com").isReachable(3000))
            {  Intent nohay= new Intent(DetallesActivity.this, noActivity.class);
            onStop();
            i = 1;
            startActivity(nohay);
            }
            else{}
        } catch (UnknownHostException e) {
             Intent nohay= new Intent(DetallesActivity.this, noActivity.class);
            onStop();   
            i = 1;
             startActivity(nohay);

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
             Intent nohay= new Intent(DetallesActivity.this, noActivity.class);
             onStop();
             i = 1;
             startActivity(nohay);


            // TODO Auto-generated catch block
            e.printStackTrace();
        }}




}

但为什么它在 Ginberbread 中有效?但不在 ICS 中?

4

1 回答 1

0

您是否已向 Manifest 声明该活动?

<application
        android:icon="@drawable/icon_test"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
....Other activities
    <activity
            android:name=".DetallesActivity">
    </activity>
....
</application>

如果这不能解决您的问题,请发布代码。

于 2013-08-12T01:07:32.737 回答