2013-07-14 3 views
0

인스턴트 메신저 화면을 사용하여 앱 시작을 계획하는 사용자 선호도 (최종 목표는 버튼에 onclick 리스너를 추가하지 않음)에 따라 달라 지므로 라디오를 처음 사용하기 위해 단계별로 시작합니다. 버튼을 기본 설정으로 사용하려면라디오 버튼 사용자 정의 환경 설정

앱을 실행하고 옵션 메뉴에서 환경 설정 버튼을 누르면 아래 코드와 같이 닫습니다.

xml 폴더의 preference.xml에서 listpreference를 통해 수행 할 수 있음을 알고 있지만 가능하다면 레이아웃에서 ID를 수행하고 싶습니다.

어떤 도움이, 감사

Splash.java

public class Splash extends Activity { 
MediaPlayer ourSong; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);   
     ourSong.start();    
     Thread timer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(2000); 
        } 
        catch (InterruptedException e){ 
        e.printStackTrace(); 
        } 
        finally{ 
         Intent intent = new Intent(Splash.this, MainActivity.class);          
         startActivity(intent); 
         } 
       } 
      };                   
      timer.start(); 
      }  
    @Override 
    protected void onPause() { 
       // TODO Auto-generated method stub 
     super.onPause(); 
     ourSong.release(); 
     finish(); 
       } 
      } 

MainActivity.java

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
@Override 
public boolean onCreateOptionsMenu(android.view.Menu menu) { 

MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.cool_menu, menu); 

getLayoutInflater().setFactory(new Factory() { 
public View onCreateView(String name, Context context,AttributeSet attrs) { 

if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { 
try { 
LayoutInflater li = LayoutInflater.from(context); 
final View view = li.createView(name, null, attrs); 

new Handler().post(new Runnable() { 
public void run() { 

((TextView) view).setTextSize(25); 
((TextView) view).setTextColor(Color.RED); 
} 
} 
);           
return view; 
} 
catch (InflateException e) {   
} 
    catch (ClassNotFoundException e) {   
    } 
}         
return null; 
} 
} 
);   
return super.onCreateOptionsMenu(menu);          
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch (item.getItemId()) { 
    case R.id.aboutUs: 
     Intent i = new Intent("com.example.custompreference.ABOUT"); 
     startActivity(i); 
    break; 
    case R.id.preferences: 
     Intent p = new Intent("com.example.custompreference.PREFS"); 
     startActivity(p); 
    break; 
    case R.id.exit: 
     finish(); 
    break; 
} 
return false; 
} 
} 

Prefs.java

을 이해할 수있을 것이다 16,
public class Prefs extends PreferenceActivity{  
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.pref); 
    } 
    } 

pref.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:gravity="center">  

<RadioButton android:id="@+id/radio_first" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="First" 
    android:textColor="#B22222" 
    android:onClick="onRadioButtonClicked"/> 

<RadioButton android:id="@+id/radio_second" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Second" 
    android:onClick="onRadioButtonClicked"/> 
</RadioGroup> 

의 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.custompreference" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.custompreference.Splash" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" >   
     <intent-filter> 
      <action android:name="com.example.custompreference.MAINACTIVITY" />     
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".AboutUs" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.example.custompreference.ABOUT" />     
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Prefs" 
     android:label="@string/app_name" >   
     <intent-filter> 
      <action android:name="com.example.custompreference.PREFS" />    
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    </application> 
    </manifest> 

로그 캣 :

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.custompreference/com.example.custompreference.Prefs}: 
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 
    'android.R.id.list' 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3687) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.RuntimeException: Your content must have a ListView whose id 
    attribute is 'android.R.id.list' 
at android.app.ListActivity.onContentChanged(ListActivity.java:243)  
    atandroid.preference.PreferenceActivity.onContentChanged(PreferenceActivity.java:165)  
at om.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:219) 
at android.app.Activity.setContentView(Activity.java:1660) 
at com.example.custompreference.Prefs.onCreate(Prefs.java:11) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 

답변

0
,logcat을 살펴보면 ListView가 필요함을 알 수 있습니다.

하지만이 모든 일이 잘못 될 것입니다. 기본적으로 PreferenceActivity에서 원하는 기본 설정 위젯을 지정하는 Preferences.xml 파일을 만들어야합니다 (예 : RadioButtons를 다음과 같이 당신의 PreferenceActivity를 해당 XML 파일을 지정 : preferences.xml로의

addPreferencesFromResource(R.xml.preferences); 

예를 this tutorial에서 그래서

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
<PreferenceCategory 
    android:summary="Username and password information" 
    android:title="Login information" > 
    <EditTextPreference 
    android:key="username" 
    android:summary="Please enter your login username" 
    android:title="Username" /> 
    <EditTextPreference 
    android:key="password" 
    android:summary="Enter your password" 
    android:title="Password" /> 
</PreferenceCategory> 

<PreferenceCategory 
    android:summary="Username and password information" 
    android:title="Settings" > 
    <CheckBoxPreference 
    android:key="checkBox" 
    android:summary="On/Off" 
    android:title="Keep me logged in" /> 

    <ListPreference 
    android:entries="@array/listOptions" 
    android:entryValues="@array/listValues" 
    android:key="listpref" 
    android:summary="List preference example" 
    android:title="List preference" /> 
</PreferenceCategory> 
</PreferenceScreen> 

(파일이 입술에 자신을 만들어야합니다/XML 폴더를 간다) 살펴 here

편집 :

당신은이 같은 preferences.xml로에서의 ListView에 사용자 정의 항목을 추가 할 수 있습니다 : 01,

<Preference 
     android:layout="@layout/your_custom_pref_entry" 
     android:key="SOME_KEY_NAME" /> 

RadioButtons 선택 사항을 처리하는 방법을 잘 모르겠습니다.

+0

xml 폴더에 preferences.xml을 작성하면 해당 목록 환경 설정에서 radiobuttons가 제공되지만 레이아웃을 통해이를 수행하고이를 사용자 정의 할 수 있다는 것을 알고 있습니다.이게 내 소중한 것입니다. 감사합니다. –

+0

수정 된 답변. – Jakob

관련 문제