2012-12-14 158 views
0

我是android開發新手,很抱歉如果問題不明確。 我想使用這個小部件「數字選擇器」,它可以在這個link Github上找到。我知道有一種方法可以將eclipse項目導入到eclipse中,但是我想要做的是將其添加到已存在的項目中。我曾嘗試手動將類和所有包含的文件夾添加到我的項目的該小部件,問題是沒有解釋如何將此數字選擇器元素添加到該活動的xml文件。 以下是我做到的: 1.將Scroller和NumberPicker類添加到項目包下的src文件夾中。 2.添加可繪製的xml,valuse和圖像。 3.將jar文件添加到Android Dependencies文件夾。 4.已經加入這個活動的xml文件:從Github到現有的Android項目

<com.example.mathgame.NumberPicker 
android:id="@+id/numberPicker" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 

,這對主要活動類:

package com.example.mathgame; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import com.example.mathgame.NumberPicker; 

public class MainActivity extends Activity { 

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

     final NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker); 
     np.setMaxValue(24); 
     np.setMinValue(1); 
     np.setFocusable(true); 

     np.setFocusableInTouchMode(true); 

    } 


} 

當我運行的應用程序,這是從我的logcat中除:

12-14 20:23:26.283: E/AndroidRuntime(29792): FATAL EXCEPTION: main 
12-14 20:23:26.283: E/AndroidRuntime(29792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathgame/com.example.mathgame.MainActivity}: java.lang.ClassCastException: android.widget.NumberPicker cannot be cast to com.example.mathgame.NumberPicker 

我想知道什麼是萊特的方式來使用進口github項目,我該怎麼做才能解決這個問題。謝謝!!!


我發現應用程序崩潰的原因。這是因爲number_picker.xml文件,我在一些地方已經更改爲com.example.mathgame.NumberPicker。現在應用程序啓動,但數字選擇器看起來很奇怪(我有按鈕和附近的一些元素)。這裏它現在看起來如何:

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ImageButton android:id="@+id/np_increment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="?attr/numberPickerUpButtonStyle" 
     android:contentDescription="@string/np_number_picker_increment_button" /> 

    <view class="com.example.mathgame.NumberPicker$CustomEditText" 
     android:id="@+id/np_numberpicker_input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="?attr/numberPickerInputTextStyle" /> 

    <ImageButton android:id="@+id/np_decrement" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="?attr/numberPickerDownButtonStyle" 
     android:contentDescription="@string/np_number_picker_decrement_button" /> 

</merge> 
+0

chave你試過了'com.example.mathgame.NumberPicker np =(com.example.mathgame.NumberPicke)findViewById(R.id.numberPicker);' –

+0

如果你確定你沒有'import android.widget .NumberPicker;'在MainActivity中,嘗試清理你的項目。 – Sam

+0

我修改了一些問題後編輯了這個問題 – vlio20

回答

1

這是一個庫項目。所以你可以直接在你的android proect中添加這個項目。 只需導入一個庫項目。 然後轉到項目屬性 - > android->向下滾動 添加庫項目。 畢竟使用這個庫類來創建對象。

+0

你能解釋一下如何將這個庫項目添加到我的項目中? – vlio20

+0

去項目屬性 - > android->向下滾動添加看到添加庫項目 –

相關問題