2013-04-25 188 views
0

我是一名初学者到android开发并遇到问题。我做了一个简单的应用程序,以列表视图显示总统。但是当我尝试在模拟器上运行它时,ir会得到此错误:Source not Found net.learn2develop.Listfragmentexample.ListFragmentExampleActivity。这里是我的java文件代码:资源未找到main.xml Eclipse

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 

H ere是我的舱单代码:

<?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> 

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

+0

你可以发布更多的logcat跟踪吗? – StoneBird 2013-04-25 23:43:32

+0

ummm。其时间长了。你需要多少条线? – 2013-04-25 23:45:49

+0

编辑你的问题并发布到那里。也许第一个10〜20行左右。只想看到回拨跟踪 – StoneBird 2013-04-25 23:48:01

回答

0

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

名称“ListFragmentExample”代表您的软件包名称,并且需要在您的项目文件中保持一致。如果你希望它是“Listfragmentexample”,那么当你想引用你的包中的东西时,它就需要在你的项目中到处都是,因为这是包名。不应混淆和混淆。

我测试过它,它在我的模拟器上工作。

0

正确的语法是@android:id/list而不是@id/android:list

而且,我会写listfragmentexample而不是Listfragmentexample和代替ListFragmentExample。你的main.xml文件和你的清单文件之间的大小写名称确实不一致。此外,个人而言,我甚至不会把这个词变成大写。

这应该照顾你的错误。

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

更新:

从你聊天,似乎的确Stonebird拿起包名参考不一致的资本,它只是这种不一致的资本需要的不仅仅是清单活动更要纠正安卓:名称。

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

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

Refactor > Rename"net.learn2develop.listfragmentexample"代替"net.learn2develop.Listfragmentexample"请一定要检查,说的复选框更新所有参考资料并预览您的更改(不要担心之后发生的错误,还需要做一些工作)。如果它要求您更新配置启动文件,请接受该文件。将所有包名称都改为小写(ClassName部分除外)是Java中的一种好习惯,因为那样你就不需要开始记住你在什么情况下使用了什么特定情况。

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

在main.xml中

然后,写 "net.learn2develop.listfragmentexample.Fragment1"代替"net.learn2develop.ListFragmentExample.Fragment1""net.learn2develop.listfragmentexample.Fragment2",而不是"net.learn2develop.ListFragmentExample.Fragment2"(通常,Eclipse的重构需要重命名这些属性的照顾,但在这种情况下,并没有因为这个资本化的错误情况甚至不是你的第一个大写的错误一致,这样算下来其搜索时,它不会对回暖和替换)

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

package net.learn2develop.listfragmentexample;而不是

package net.learn2develop.Listfragmentexample;(这个部分应该已经被Eclipse照顾过了,但这是你应该仔细检查以防万一)。

之后,在每个java文件中执行Ctrl-shift-o(组织导入)。如果您在正确重置软件包名称之前过早执行了此步骤,则会导入android.R(即您想要的不是)。确保import android.R;确实不是出现在您的Java文件的任何地方。如果它的存在,删除这一行,做一个按Ctrl-Shift-O组合(组织进口),直到你自己的研发导入和的android.R

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

+0

非常感谢石头鱼和斯蒂芬!这让我有一阵子阻挠了。并感谢您一直以来帮助我! – 2013-04-26 23:29:57

+0

@CadenRatcliff谁是“石鱼”?大声笑 – StoneBird 2013-04-27 01:37:50

+0

StoneBird!抱歉!大声笑 – 2013-04-29 23:51:04