星级应用程序在模拟器上运行,但不在设备上运行

时间:2014-09-13 08:29:03

标签: android sqlite android-emulator

MainActivity.java

package com.example.popup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.example.popup.DatabaseHelper;
import com.example.popup.Rating;

public class MainActivity extends Activity {
    private static final int LENGTH_LONG = 0;
    private TextView tv1;
    private RatingBar rb1;
    private LayerDrawable stars;
    DatabaseHelper db;
    String IMEIid;
    int value;

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

        db = new DatabaseHelper(getApplicationContext());

        Button showPopUpButton = (Button) findViewById(R.id.buttonShowPopUp);
        showPopUpButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int k = showSimplePopUp();
                value = k;
                }
            });

        TextView textDeviceID = (TextView) findViewById(R.id.deviceid);

        // retrieve a reference to an instance of TelephonyManager
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        IMEIid = getDeviceID(telephonyManager);

        db.closeDB();
    }

    private int showSimplePopUp() {

        AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popup_layout, null);

        tv1 = (TextView) layout.findViewById(R.id.textView1);
        rb1 = (RatingBar) layout.findViewById(R.id.ratingBar1);
        stars = (LayerDrawable) rb1.getProgressDrawable();
        Log.d("hi","in showpopup");
        helpBuilder.setView(layout);

        rb1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float ratingValue,
                    boolean fromUser) {

                tv1.setText(String.valueOf(ratingValue));
                value = (int) (ratingValue) - 6;
                tv1.setText(String.valueOf(value));

                int color = Color.BLUE;

                if (value > 0)
                    color = Color.GREEN;
                else if (value < 0)
                    color = Color.RED;

                stars.getDrawable(2).setColorFilter(color,
                        PorterDuff.Mode.SRC_ATOP);

            }
        });

        helpBuilder.setTitle("Rate The Chat");

        helpBuilder.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("hi","in on click");
                        Rating r = new Rating(IMEIid, value);
                        Log.d(r.id, "msgsdfgsdfg");

                        long j = db.createRating(r);
                        if (j == -1) {
                            Toast.makeText(getApplicationContext(),
                                    "Error in storing the rating", LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "Thanks For rating the Chat", LENGTH_LONG).show();
                        }
                    }
                });

        helpBuilder.setNegativeButton("No Thanks",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                });
        AlertDialog helpDialog = helpBuilder.create();
        helpDialog.show();

        return value;
    }

    String getDeviceID(TelephonyManager phonyManager) {

        String id = phonyManager.getDeviceId();
        if (id == null) {
            id = "not available";
        }

        int phoneType = phonyManager.getPhoneType();
        switch (phoneType) {
        case TelephonyManager.PHONE_TYPE_NONE:
            return "NONE: " + id;

        case TelephonyManager.PHONE_TYPE_GSM:
            return "GSM: IMEI=" + id;

        case TelephonyManager.PHONE_TYPE_CDMA:
            return "CDMA: MEID/ESN=" + id;

        default:
            return "UNKNOWN: ID=" + id;
        }

    }

}

Databasehelperclass.java

package com.example.popup;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper{
    static int sno = 0;

    private static final String LOG = "DatabaseHelper";

    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_NAME = "Ratingbar";//DataBase Name

    private static final String TABLE_RATING = "rating5";//Table Name

    private static final String KEY_ID = "id";

    private static final String KEY_VALUE = "rate";

    private static final String CREATE_TABLE_RATING = "CREATE TABLE IF NOT EXISTS "     
            + TABLE_RATING + "(sno INTEGER, " + KEY_ID + " TEXT, " + KEY_VALUE
            + " INTEGER)";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {

           db.execSQL(CREATE_TABLE_RATING);
        }

    public long createRating(Rating r) {
        SQLiteDatabase db = this.getWritableDatabase();
        Log.d(r.id, "imei value");
        ContentValues values = new ContentValues();
        values.put("sno", sno=sno+1);
        values.put(KEY_ID, r.id);
        values.put(KEY_VALUE, r.value);
        Log.d("hi","Before insertion");
        long Rating_id = db.insert(TABLE_RATING, null, values);
        Log.i("hi","hello"+Rating_id);
        return Rating_id;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
           db.execSQL("drop table if exists " + TABLE_RATING);
        onCreate(db);
    }

    public void closeDB() {
        SQLiteDatabase db = this.getReadableDatabase();
        if (db != null && db.isOpen())
            db.close();
    }
       }

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:numStars="11"
        android:stepSize="1.0"
        android:isIndicator="false"
        style="?android:attr/ratingBarStyleSmall"/>
        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Selected rating: " />
 </LinearLayout>

以上代码用于弹出星级评级。它在模拟器中工作正常。但是当它在移动设备上执行时,它没有被执行。它在移动设备中完美地显示视图,但它没有将数据存储在数据库,它也没有在logcat中显示任何内容。有人知道如何实现这一目标吗?

0 个答案:

没有答案