0

我是 android 开发的初学者并且遇到了问题。我正在制作一个简单的应用程序,在列表视图中显示总统。但是当我尝试在模拟器上运行它时,我得到了这个错误:Source not Found net.learn2develop.Listfragmentexample.ListFragmentExampleActivity。这是我的java文件代码:

package net.learn2develop.Listfragmentexample;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends ListFragment {
String[] presidents = {
        "Dwight D. Eisenhower",
        "John F. Kennedy",
        "Lyndon B. Johnson",
        "Richard Nixon",
        "Gerald Ford",
        "Jimmy Carter",
        "Ronald Reagen",
        "George H. W. Bush",
        "Bill Clinton",
        "George W. Bush",
        "Barack Obama"
};

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, container, false);
}
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, presidents));
    }
        public void onListItemClick(ListView parent, View v,
                int position, long id)
        {   
            Toast.makeText(getActivity(),
                    "You have selected item : " + presidents[position],
                    Toast.LENGTH_SHORT).show();
        }
    }

这是 main.xml 的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<fragment
    android:id="@+id/fragment1"
    android:name="net.learn2develop.ListFragmentExample.Fragment1"
    android:layout_weight="0.5"
    android:layout_width="0dp"
    android:layout_height="200dp" />

<fragment
    android:id="@+id/fragment2"
    android:name="net.learn2develop.ListFragmentExample.Fragment1"
    android:layout_weight="0.5"
    android:layout_width="0dp"
    android:layout_height="300dp" />

</LinearLayout>

这是 fragment1.xml 的代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@id/android:list"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:drawSelectorOnTop="false"/>
</LinearLayout>

这是我的 Logcat 跟踪:

[2013-04-25 18:31:55 - ListFragmentExample] Android Launch!
[2013-04-25 18:31:55 - ListFragmentExample] adb is running normally.
[2013-04-25 18:31:55 - ListFragmentExample] Performing           net.learn2develop.Listfragmentexample.ListFragmentExampleActivity activity launch
[2013-04-25 18:31:55 - ListFragmentExample] Automatic Target Mode: using existing  emulator 'emulator-5554' running compatible AVD 'Android_4.0'
[2013-04-25 18:31:55 - ListFragmentExample] Uploading ListFragmentExample.apk onto device 'emulator-5554'
[2013-04-25 18:31:56 - ListFragmentExample] Installing ListFragmentExample.apk...
[2013-04-25 18:32:03 - ListFragmentExample] Success!
[2013-04-25 18:32:03 - ListFragmentExample] Starting activity net.learn2develop.Listfragmentexample.ListFragmentExampleActivity on device emulator-5554
[2013-04-25 18:32:05 - ListFragmentExample] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=net.learn2develop.Listfragmentexample/.ListFragmentExampleActivity }
[2013-04-25 18:32:06 - ListFragmentExample] Attempting to connect debugger to 'net.learn2develop.Listfragmentexample' on port 8666

这是我的清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.Listfragmentexample"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="net.learn2develop.Listfragmentexample.ListFragmentExampleActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

有人可以在这里填写吗?我不知道我做错了什么。任何帮助深表感谢!

4

2 回答 2

0

正确的语法@android:id/list@id/android:list

另外,我会写listfragmentexample而不是Listfragmentexample和而不是ListFragmentExample. 您对该包名称的大写在您的 main.xml 文件和清单文件之间确实不一致。此外,就我个人而言,我什至根本不会把这个词大写。

这应该可以解决您的错误。

但就个人而言,我也会写".ListFragmentExampleActivity"而不是"net.learn2develop.Listfragmentexample.ListFragmentExampleActivity"仅仅为了简化清单。它真的不需要那么复杂。我建议的最后一个更改不会解决任何问题,它只会使清单更具可读性。

更新:

从您的聊天中,Stonebird 似乎确实注意到了包名称引用的大小写不一致,只是这种不一致的大小写需要在不仅仅是清单活动 android:name 中更正。

在清单活动 android:name 中,请按照我上面的建议进行操作。写".ListFragmentExampleActivity"而不是"net.learn2develop.Listfragmentexample.ListFragmentExampleActivity"

然后在 Eclipse 的项目资源管理器窗格中,将鼠标放在包名上,右键单击并选择:

Refactor > Rename改为确保选中更新所有引用并预览您的更改"net.learn2develop.listfragmentexample""net.learn2develop.Listfragmentexample"复选框(不要担心之后会出现错误,仍然需要做一些工作)。如果它要求您更新配置启动文件,也请接受。在 Java 中,将所有包名转为小写(ClassName 部分除外)是一个好习惯,因为这样您就不需要开始记住您在什么上使用了哪种特定大小写。

之后,在清单的第二行或第三行,确保写"net.learn2develop.listfragmentexample"而不是"net.learn2develop.Listfragmentexample"(我不认为 Eclipse Refactor 命令处理了那个)。

然后在你的 main.xml 中,写 "net.learn2develop.listfragmentexample.Fragment1"而不是 "net.learn2develop.ListFragmentExample.Fragment1" 和写"net.learn2develop.listfragmentexample.Fragment2"而不是"net.learn2develop.ListFragmentExample.Fragment2"(通常,Eclipse Refactor 负责重命名这些属性,但在这种情况下它没有,因为这种情况下的大写错误甚至与你的第一个大写错误不一致,所以它在进行搜索和替换时不会注意到这一点)

最后,在你所有的 java 文件中,确保它是这样写的:

package net.learn2develop.listfragmentexample;代替

package net.learn2develop.Listfragmentexample;(这部分应该由 Eclipse 处理,但您应该仔细检查以防万一)。

之后,在每个 java 文件中执行 Ctrl-shift-o(以组织导入)。如果您在正确重置包名称之前过早执行该步骤,那么它将导入 android.R(这不是您想要的)。确保它import android.R;不会出现在您的 java 文件中的任何位置。如果它在那里,请删除该行,然后执行 Ctrl-shift-o(组织导入),直到导入您自己的 R 而不是android.R

之后,用鼠标选择Project > Clean...Source > Clean...(我忘记了哪个菜单)。如果 Eclipse 出现错误,请打开 Eclipse 错误视图窗格以确保您没有错过任何其他需要更改以前的包名称的地方。在 Error View 窗格中,您可以双击每个有问题的错误以将您带到 Eclipse 认为错误所在的位置。

于 2013-04-26T01:54:04.283 回答
0

阅读您的代码后,我将每个“Listfragmentexample”更改为“ListFragmentExample”。这发生在多个文件中。

名称“ListFragmentExample”代表您的包名称,并且需要在您的项目文件中保持一致。如果您希望它是“Listfragmentexample”,那么当您想要引用包中的某些内容时,它需要在项目中的任何地方都像这样,因为那是包名称。不允许混搭。

我已经对其进行了测试,它可以在我的模拟器上运行。

于 2013-04-27T01:37:22.643 回答