小编are*_*ing的帖子

findViewById 未定义

Eclipse 将 findViewById(int) 标记为未定义;它对 getResources() 做了同样的事情,但我能够通过调用 context.getResources() 来解决这个问题(如下所示),并且似乎无法为 findViewById 找到类似的解决方法。这是代码:

package com.myapp.android.MyWidget;

import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Button;
import android.os.Bundle;

public class MyWidget extends AppWidgetProvider {
 private static String[] states;

 @Override
 public void onEnabled(Context context) {
  final Button button = (Button) findViewById(R.id.widget_state_button);

  states = context.getResources().getStringArray(R.array.states);
 }
Run Code Online (Sandbox Code Playgroud)

我需要为 findViewById 导入另一个包吗?提前致谢。

android android-widget

5
推荐指数
1
解决办法
3万
查看次数

为Manifest xml中列出的PreferenceActivity抛出的ActivityNotFound异常

我正在尝试PreferenceActivity在我的应用程序中实现一个,但我不断得到类型的例外android.content.ActivityNotFoundException.它提到确保Activity列在我的Manifest文件中,但它确实存在并且我仍然得到例外.在此先感谢您的帮助 - 这一直让我发疯.

这是一个类似的帖子我发现有一个解决方案,但它对我不起作用: 我的PreferenceActivity没有显示,即使它在我的清单文件中

RES/XML /的preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/preferences_title_settings"
        android:key="@string/preferences_key_settings" >
    <PreferenceScreen android:summary="@string/preferences_summary_gameplaySettings"
            android:title="@string/preferences_title_gameplaySettings"
            android:key="@string/preferences_key_gameplaySettings">            
        <ListPreference android:entries="@array/entries_difficulty"
                android:entryValues="@array/entryvalues_difficulty"
                android:dialogTitle="@string/dialog_title_difficulty"
                android:title="@string/preferences_title_difficulty"
                android:key="@string/preferences_key_difficulty"
                android:summary="@string/preferences_summary_difficulty"
                android:defaultValue="0" />
        <CheckBoxPreference android:key="@string/preferences_key_autosave"
                android:title="@string/preferences_title_autosave"
                android:summary="@string/preferences_summary_autosave" />
    </PreferenceScreen>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

Preferences.java:

public class Preferences extends PreferenceActivity implements 
    OnSharedPreferenceChangeListener {
        private SharedPreferences preferences;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            addPreferencesFromResource(R.xml.preferences);
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this)
        }
    }
Run Code Online (Sandbox Code Playgroud)

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.areyling.myapp"
      android:versionCode="1"
      android:versionName="@string/app_version" >
    <uses-sdk android:minSdkVersion="4" /> …
Run Code Online (Sandbox Code Playgroud)

android android-activity

1
推荐指数
1
解决办法
2569
查看次数

标签 统计

android ×2

android-activity ×1

android-widget ×1