Android偏好不会在应用中生效

时间:2013-07-10 13:07:02

标签: android android-intent

我是Android新手。 我正在编写一个应用程序,它取决于用户输入的URL,以便它工作。我的主要活动中有代码,用于检测是否已设置用户首选项。如果没有,则会打开编辑首选项活动。

然而,我的应用程序必须至少崩溃一次或两次才能“注册”我的新pref设置。我怎么能纠正这个?

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    // integrate our shared preferences settings
    sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    prefix = sharedPrefs.getString("manual_fetch_domain", "");

    if (prefix.length() < 7) {
        startActivity(new Intent(this, EditSettingsActivity.class));
    }
}

public void onPrimaryClick(View view) {
    Intent intent = new Intent(getApplicationContext(),
            PrimaryImageGridActivity.class);

    //confirm our settings
    if (prefix.length() < 7) {
        startActivity(new Intent(this, EditSettingsActivity.class));
    }

    String readTwitterFeed = readTwitterFeed(prefix + Constants.SERVER_URL);
    List<String> where = new ArrayList<String>();
    int k = 0;

    if (readTwitterFeed != null) {
        //code here
    }

}

public String readTwitterFeed(String y) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(y);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content), 16384);
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // error check again
    String tmp = builder.toString();
    if (tmp.length() < 2)
        return null;
    else
        return builder.toString();
}

我的日志猫的部分如下:

07-10 13:23:18.818: E/AndroidRuntime(767): FATAL EXCEPTION: main
07-10 13:23:18.818: E/AndroidRuntime(767): java.lang.IllegalStateException: Could not execute method of the activity
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3599)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.view.View.performClick(View.java:4204)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.view.View$PerformClick.run(View.java:17355)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.os.Handler.handleCallback(Handler.java:725)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 13:23:18.818: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
07-10 13:23:18.818: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
07-10 13:23:18.818: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 13:23:18.818: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 13:23:18.818: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
07-10 13:23:18.818: E/AndroidRuntime(767): Caused by: java.lang.reflect.InvocationTargetException
07-10 13:23:18.818: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
07-10 13:23:18.818: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:511)
07-10 13:23:18.818: E/AndroidRuntime(767):  at android.view.View$1.onClick(View.java:3594)
07-10 13:23:18.818: E/AndroidRuntime(767):  ... 11 more
07-10 13:23:18.818: E/AndroidRuntime(767): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=/
07-10 13:23:18.818: E/AndroidRuntime(767):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
07-10 13:23:18.818: E/AndroidRuntime(767):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
07-10 13:23:18.818: E/AndroidRuntime(767):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-10 13:23:18.818: E/AndroidRuntime(767):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-10 13:23:18.818: E/AndroidRuntime(767):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-10 13:23:18.818: E/AndroidRuntime(767):  at com.src.shady.MainActivity.readTwitterFeed(MainActivity.java:118)
07-10 13:23:18.818: E/AndroidRuntime(767):  at com.src.shady.MainActivity.onPrimaryImageGridClick(MainActivity.java:67)

1 个答案:

答案 0 :(得分:1)

我猜您已将onClick添加到 layout.xml 文件中,但是:

  • layout.xml 设置为activity并且未声明onClick(处理程序不存在)
  • layout.xml 设置为对话框(这是错误的,您需要添加OnClickListener来处理操作)