2

我正在自学编写 android 应用程序,我写了一个简单的小费计算器作为入门项目。该程序在模拟器上运行良好,但是当我导出 apk 文件时,它不会安装在我的手机上。我在没有 adb 的情况下传输了文件,因为 adb 不会检测到设备。我在 ubuntu 12.04 上,我在 udev 规则中有供应商代码,设备是华为 Ascend (M860) 我怎样才能找到出错的日志。或者我做错了什么的想法。

这是主要的java文件:

package com.groundscore.gstipcalc;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

// Constants used when saving and restoring

private static final String TOTAL_BILL = "TOTAL_BILL";
private static final String CURRENT_TIP = "CURRENT_TIP";
private static final String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";

private double billBeforeTip;   // Users bill before tip
private double tipAmount;   // Tip amount
private double finalBill;   // Bill plus Tip

EditText billBeforeTipET;
EditText tipAmountET;
EditText finalBillET;

SeekBar tipSeekBar;

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

    setContentView(R.layout.activity_main);// Inflate the GUI

    // Check if app just started, or if it is being restored        
    if(savedInstanceState == null){

        billBeforeTip = 0.0;
        tipAmount = .15;
        finalBill = 0.0;

    } else {
        // App is being restored

        billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
        tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
        finalBill = savedInstanceState.getDouble(TOTAL_BILL);

    }

    // Initialize the EditTexts

    billBeforeTipET = (EditText) findViewById(R.id.billEditText);
    tipAmountET = (EditText) findViewById(R.id.tipEditText);
    finalBillET = (EditText) findViewById(R.id.finalEditText);

    tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);

    tipSeekBar.setOnSeekBarChangeListener(tipSeekBarChangeListener);

    billBeforeTipET.addTextChangedListener(billBeforeTipListener);

}

private OnSeekBarChangeListener tipSeekBarChangeListener = new OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {

        tipAmount = (tipSeekBar.getProgress()) * .01;

        tipAmountET.setText(String.format("%.02f", tipAmount));

        updateTipAndFinalBill();

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }


};

private TextWatcher billBeforeTipListener = new TextWatcher(){

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        try{

            billBeforeTip = Double.parseDouble(s.toString());
        }catch(NumberFormatException e){

            billBeforeTip = 0.0;
        }

        updateTipAndFinalBill();

    }


};



private void updateTipAndFinalBill() {

    double tipAmount = Double.parseDouble(tipAmountET.getText().toString());

    double finalBill = billBeforeTip + (billBeforeTip * tipAmount);

    finalBillET.setText(String.format("%.02f", finalBill));
}

protected void onSaveInstanceState(Bundle outState){

    super.onSaveInstanceState(outState);

    outState.putDouble(TOTAL_BILL, finalBill);
    outState.putDouble(CURRENT_TIP, tipAmount);
    outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是布局:

<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:padding="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".GsTipCalc" >

<TextView
    android:id="@+id/billTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="15dp"
    android:text="@string/bill_text_view" >
</TextView>

<EditText
    android:id="@+id/billEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="5"
    android:hint="@string/bill_edit_text"
    android:inputType="numberDecimal"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="11dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="38dp" />

<EditText
    android:id="@+id/finalEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/changeTipSeekBar"
    android:layout_alignLeft="@+id/billTextView"
    android:ems="5"
    android:hint="@string/bill_edit_text"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/finalTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/finalEditText"
    android:layout_alignLeft="@+id/finalEditText"
    android:text="@string/final_text_view" />

<TextView
    android:id="@+id/tipTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/tipEditText"
    android:layout_alignLeft="@+id/tipEditText"
    android:text="@string/tip_text_view" />

<SeekBar
    android:id="@+id/changeTipSeekBar"
    android:layout_width="89dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/tipEditText"
    android:layout_marginRight="37dp"
    android:layout_marginTop="84dp"
    android:progress="15" />

<EditText
    android:id="@+id/tipEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/billEditText"
    android:layout_alignBottom="@+id/billEditText"
    android:layout_alignLeft="@+id/changeTipSeekBar"
    android:ems="3"
    android:inputType="numberDecimal"
    android:text="@string/tip_edit_text" />

<TextView
    android:id="@+id/SlideTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/finalEditText"
    android:layout_alignLeft="@+id/changeTipSeekBar"
    android:text="@string/change_tip_text_view" />

</RelativeLayout>

这是从安装尝试中读出的 Logcat:

I/ActivityManager( 7588): Starting: Intent { dat=file:///mnt/sdcard/GsTipCalc.apk cmp=com.android.packageinstaller/.InstallAppProgress (has extras) } from pid 14921
I/ActivityManager( 7588): Displayed com.android.packageinstaller/.InstallAppProgress: +347ms
D/dalvikvm(14940): GC_EXPLICIT freed 4K, 50% free 2696K/5379K, external 0K/0K, paused 275ms
W/ActivityManager( 7588): No content provider found for: 
D/VoldCmdListener(  207): asec list
I/PackageHelper(14940): Size of container 2 MB 234323 bytes
D/VoldCmdListener(  207): asec create smdl2tmp1 2 fat {} 10022
W/logwrapper(14978): Unable to background process (No such file or directory)
I//system/bin/newfs_msdos(  207): /system/bin/newfs_msdos: warning, /dev/block/dm-2 is not a character device
I//system/bin/newfs_msdos(  207): /system/bin/newfs_msdos: Skipping mount checks
I//system/bin/newfs_msdos(  207): Bogus heads from kernel - setting sane value
I//system/bin/newfs_msdos(  207): Bogus sectors from kernel - setting sane value
I//system/bin/newfs_msdos(  207): /dev/block/dm-2: 4176 sectors in 522 FAT32 clusters (4096 bytes/cluster)
I//system/bin/newfs_msdos(  207): bps=512 spc=8 res=32 nft=2 sec=4221 mid=0xf0 spt=63 hds=64 hid=0 bspf=5 rdcl=2 infs=1 bkbs=2
I/Vold    (  207): Filesystem formatted OK
D/VoldCmdListener(  207): asec path smdl2tmp1
I/PackageHelper(14940): Created secure container smdl2tmp1 at /mnt/asec/smdl2tmp1
I/DefContainer(14940): Created container for smdl2tmp1 at path : /mnt/asec/smdl2tmp1
I/DefContainer(14940): Copied /mnt/sdcard/GsTipCalc.apk to /mnt/asec/smdl2tmp1/pkg.apk
D/VoldCmdListener(  207): asec finalize smdl2tmp1
I/DefContainer(14940): Finalized container smdl2tmp1
I/DefContainer(14940): Unmounting smdl2tmp1 at path /mnt/asec/smdl2tmp1
D/dalvikvm(14940): GC_EXPLICIT freed 17K, 50% free 2695K/5379K, external 0K/0K, paused 71ms
D/dalvikvm( 7588): GC_EXPLICIT freed 227K, 38% free 6615K/10631K, external 609K/1800K, paused 169ms
D/VoldCmdListener(  207): asec unmount smdl2tmp1 force
W/ActivityManager( 7588): No content provider found for: 
D/VoldCmdListener(  207): asec mount smdl2tmp1 {} 1000
D/VoldCmdListener(  207): asec path smdl2tmp1
D/PackageParser( 7588): Scanning package: /mnt/asec/smdl2tmp1/pkg.apk
E/PackageParser( 7588): Package com.groundscore.gstipcalc has no certificates at entry res/layout/activity_main.xml; ignoring!
I/PackageHelper( 7588): Forcibly destroying container smdl2tmp1
D/dalvikvm( 7588): GC_EXPLICIT freed 135K, 39% free 6530K/10631K, external 609K/1800K, paused 169ms
D/VoldCmdListener(  207): asec destroy smdl2tmp1 force
D/dalvikvm( 7588): GC_EXPLICIT freed 14K, 39% free 6528K/10631K, external 609K/1800K, paused 245ms
4

3 回答 3

2

我想您可能已经使用了“项目右键单击 -> Android 工具 -> 导出未签名的应用程序包”并无意中创建了一个未签名的 APK。未签名的 APK不会在模拟器和实际设备上运行。当您将应用程序从 Eclipse 运行到模拟器时,APK 会使用调试证书进行签名,因此通过这种方式,APK 仍然是签名的。

将 APK 传输到您的设备的最快方法是转到 dir /bin,那里应该有一个 APK,它会刷新您更改的所有内容并在模拟器上运行更改。

参考1

于 2013-07-09T01:16:00.563 回答
1

您的手机出现在设备列表中??

如您所知,请转到您的 adb 安装并运行 adb 工具

you_path\adt-bundle-windows-x86_64\sdk\platform-tools

adb devices

这应该列出连接到您的工具的设备。

确保模拟器已关闭。

于 2013-07-06T01:23:59.113 回答
1

minSdkVersion可能,你的,会有问题targetSdkVersion

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

minSdkVersion="8" :适用于 android 2.3

targetSdkVersion = "17" 对于 android 4.1

您的手机是 Lower SDK ,而您的 .apk 文件是使用 Higher SDK 创建的。

于 2013-07-06T19:02:53.823 回答