0

我不知道发生了什么,我在两个不同的项目中开发了这两个项目。两者都从同一个mysql表中获取数据。然后我有点将这两个项目混合在一个java文件中,之后麻烦来了。请问有人可以F1吗??

日志猫

10-25 03:04:51.024: E/AndroidRuntime(2227): FATAL EXCEPTION: main
10-25 03:04:51.024: E/AndroidRuntime(2227): java.lang.NullPointerException
10-25 03:04:51.024: E/AndroidRuntime(2227):     at com.hadafsoft.smartaddis.Music$3.onItemClick(Music.java:133)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.os.Handler.handleCallback(Handler.java:730)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.os.Looper.loop(Looper.java:137)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at java.lang.reflect.Method.invoke(Method.java:525)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-25 03:04:51.024: E/AndroidRuntime(2227):     at dalvik.system.NativeStart.main(Native Method)

音乐.java

package com.hadafsoft.smartaddis;

//all the necessary imports are imported

public class Music extends Activity {

    private String SDCardPath = "/mnt/sdcard/";

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    public ProgressDialog mProgressDialog, dialog;
    public String songSender, tempSong, tempSinger;
    public String SongServerPath, SongName;

    private ListView lstView;
    private ImageAdapter imageAdapter;

    public int currentPage = 1;
    public int TotalPage = 0;

    public Button btnNext;
    public Button btnPre;

    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.music);

        // Bets orientation
        onConfigurationChanged(null);

        // ListView and imageAdapter

        lstView = (ListView) findViewById(R.id.listView1);
        lstView.setClipToPadding(false);

        imageAdapter = new ImageAdapter(getApplicationContext());
        lstView.setAdapter(imageAdapter);

        // Next
        btnNext = (Button) findViewById(R.id.btnNext);
        // Perform action on click
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage + 1;
                ShowData();
            }
        });

        // Previous
        btnPre = (Button) findViewById(R.id.btnPre);
        // Perform action on click
        btnPre.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage - 1;
                ShowData();
            }
        });

        // Show first load
        ShowData();

        // listView1

        // OnClick
        lstView.setOnItemClickListener(new OnItemClickListener() {
            @SuppressLint("NewApi")
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // *** Check file exists in SDCard ***/
                SongServerPath = MyArrList.get(position).get("path").toString();
                String fileName = SongServerPath.substring(
                        SongServerPath.lastIndexOf('/') + 1,
                        SongServerPath.length());
                String strPath = SDCardPath + fileName; // file in sdcard Eg :/mnt/sdcard/Mary.mp3                                      

                File file = new File(strPath);

                SongName = MyArrList.get(position).get("song_en").toString();

                tempSong = MyArrList.get(position).get("song_en").toString();
                tempSinger = MyArrList.get(position).get("singer_en").toString();
                songSender = "D - "+tempSinger+" - "+tempSong;

                /*** if file exists (View for Download) ***/
                if (file.exists()) {
                    ViewMusicFileSDCard(strPath, SongName);
                } else {
                    showYesOrCancelDialog();
                }

            }
        });

        Log.i(TAG, "onCreate");

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Configuration c = getResources().getConfiguration();

        if (c.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(Music.this, "ይህ ገዕ ላንድስኬፕ አቀምመጥ የለውም", Toast.LENGTH_LONG).show();
        }
    }

    class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {

        @Override
        protected Object doInBackground(Object... params) {

            String url = "http://10.0.2.2/android/smartaddis/music/getMusic.php";

            JSONArray data;
            try {
                data = new JSONArray(getJSONUrl(url));

                MyArrList = new ArrayList<HashMap<String, Object>>();
                HashMap<String, Object> map;

                int displayPerPage = 7; // Per Page
                int TotalRows = data.length();
                int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);

                if (TotalRows <= displayPerPage) {
                    TotalPage = 1;
                } else if ((TotalRows % displayPerPage) == 0) {
                    TotalPage = (TotalRows / displayPerPage);
                } else {
                    TotalPage = (TotalRows / displayPerPage) + 1;
                    TotalPage = (int) TotalPage;
                }
                int indexRowEnd = displayPerPage * currentPage;
                if (indexRowEnd > TotalRows) {
                    indexRowEnd = TotalRows;
                }

                for (int i = indexRowStart; i < indexRowEnd; i++) {
                    JSONObject c = data.getJSONObject(i);
                    map = new HashMap<String, Object>();
                    map.put("id", (String) c.getString("id"));
                    map.put("singer_en", (String) c.getString("singer_en"));
                    map.put("song_en", (String) c.getString("song_en"));

                    map.put("length", (String) c.getString("length"));
                    map.put("releaseddate", (String) c.getString("releaseddate"));

                    // Thumbnail Get ImageBitmap To Object
                    map.put("imagepath", (String) c.getString("imagepath"));
                    Bitmap newBitmap = loadBitmap(c.getString("imagepath"));
                    map.put("ImagePathBitmap", newBitmap);

                    MyArrList.add(map);

                    publishProgress(i);
                }

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

            return null;
        }

        @Override
        public void onProgressUpdate(Integer... progress) {
            imageAdapter.notifyDataSetChanged();
        }

        @Override
        protected void onPostExecute(Object result) {

            if (currentPage >= TotalPage) {
                btnNext.setEnabled(false);
            } else {
                btnNext.setEnabled(true);
            }

            if (currentPage <= 1) {
                btnPre.setEnabled(false);
            } else {
                btnPre.setEnabled(true);
            }

            if (dialog != null)
                dialog.dismiss();
        }
    }

    class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return MyArrList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.music_column, null);
            }

            // ImagePath
            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.music_thumb);
            imageView.getLayoutParams().height = 120;
            imageView.getLayoutParams().width = 120;
            imageView.setPadding(10, 10, 0, 10);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            try {
                imageView.setImageBitmap((Bitmap) MyArrList.get(position).get(
                        "ImagePathBitmap"));
            } catch (Exception e) {
                imageView
                        .setImageResource(android.R.drawable.ic_menu_report_image);
            }

            // ID, Song, Singer,  Length and Released-Date
            TextView txtSong = (TextView) convertView
                    .findViewById(R.id.song);
            txtSong.setText(MyArrList.get(position).get("song_en").toString());
            txtSong.setPadding(10, 0, 0, 0);

            TextView txtSinger = (TextView) convertView
                    .findViewById(R.id.singer);
            txtSinger.setText(MyArrList.get(position).get("singer_en")
                    .toString());
            txtSinger.setPadding(10, 0, 0, 0);

            TextView txtLength = (TextView) convertView
                    .findViewById(R.id.length);
            txtLength.setText(MyArrList.get(position).get("length").toString());
            txtLength.setPadding(10, 0, 0, 0);

            TextView txtDate = (TextView) convertView.findViewById(R.id.releaseddate);
            txtDate.setText(MyArrList.get(position).get("releaseddate").toString());
            txtDate.setPadding(10, 0, 0, 0);


            return convertView;

        }

    }

    /*** Get JSON Code from URL ***/
    public String getJSONUrl(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Download OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download file..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    /***** Get Image Resource from URL (Start) *****/
    private static final String TAG = "Image";
    private static final int IO_BUFFER_SIZE = 4 * 1024;

    public static Bitmap loadBitmap(String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new URL(url).openStream(),
                    IO_BUFFER_SIZE);

            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            copy(in, out);
            out.flush();

            final byte[] data = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            // options.inSampleSize = 1;

            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
                    options);
        } catch (IOException e) {
            Log.e(TAG, "Could not load Bitmap from: " + url);
        } finally {
            closeStream(in);
            closeStream(out);
        }

        return bitmap;
    }

    private static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                android.util.Log.e(TAG, "Could not close stream", e);
            }
        }
    }

    private static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }
    }

    /***** Get Image Resource from URL (End) *****/

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        // webview.saveState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        // webview.restoreState(savedInstanceState);
        // lstView.restoreState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.items, menu);

        return super.onCreateOptionsMenu(menu);
    }

}
4

1 回答 1

0

在 Music.java 的第 133 行,您正在使用“路径”键访问字段。

SongServerPath = MyArrList.get(position).get("path").toString();

但我认为你没有为这个键设置值。您已设置“图像路径”但没有设置“路径”。

请检查是否有意义。

于 2013-10-25T15:46:00.400 回答