(Android)无法在同一个包中调用另一个活动

时间:2011-11-05 11:00:42

标签: android android-activity

我花了很长时间试图找到错误,但我似乎无法找到一个在线工作的答案。情况是每次单击列表中的选项时LogCat都会产生此错误:android.content.ActivityNotFoundException:无法找到显式活动类。

清单文件:

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

    <uses-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".ProcessCaptureActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FileData" android:label="@string/app_name">
        </activity>
        <service android:name=".BackgroundCapture" android:enabled="true" android:process=":Background" android:permission="android.permission.WAKE_LOCK" android:label="@string/app_name"></service>
        <receiver android:name=".BootReceiver" android:label="@string/app_name" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

ProcessCaptureActivity.java(主类):

package com.capture.ProcessCapture;
... (all the imports)

public class ProcessCaptureActivity extends ListActivity {
    private File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ProcessCapture/data");
    private String accessedFile;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onResume();
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, capturedFiles()));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              // Get file name:
                accessedFile = ((TextView) view).getText().toString();
                Intent newIntent = new Intent(view.getContext(), FileData.class);
                newIntent.putExtra("filename", accessedFile);
                startActivity(newIntent);
            }
          });
    }

    public void onResume() {
        super.onResume();
        //Timeout for 30min (Using AlarmManager)
        Intent myIntent = new Intent(this, BackgroundCapture.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
    }

    private String[] capturedFiles() {
        return dir.list();
    }

}

FileData.java

package com.capture.ProcessCapture;

... (all the imports)

public class FileData extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File sdcard = Environment.getExternalStorageDirectory();

        Bundle bundle = this.getIntent().getExtras();
        String filename = bundle.getString("filename");
        File file = new File(sdcard+"/ProcessCapture/data", filename);

        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        TextView tv = (TextView) findViewById(R.id.file_data);
        tv.setText(text);

        setContentView(R.layout.file_data);
    }

}

完整LogCat :(仅在单击列表项时显示错误)

11-05 10:45:28.743: E/AndroidRuntime(907): FATAL EXCEPTION: main
11-05 10:45:28.743: E/AndroidRuntime(907): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.capture.ProcessCapture/com.capture.ProcessCapture.FileData}; have you declared this activity in your AndroidManifest.xml?
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.app.Activity.startActivityForResult(Activity.java:2827)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.app.Activity.startActivity(Activity.java:2933)
11-05 10:45:28.743: E/AndroidRuntime(907):  at com.capture.ProcessCapture.ProcessCaptureActivity$1.onItemClick(ProcessCaptureActivity.java:37)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.widget.ListView.performItemClick(ListView.java:3513)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.os.Handler.handleCallback(Handler.java:587)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:123)
11-05 10:45:28.743: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-05 10:45:28.743: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:45:28.743: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:507)
11-05 10:45:28.743: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-05 10:45:28.743: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-05 10:45:28.743: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)

提前致谢!抱歉,长问题=)

2 个答案:

答案 0 :(得分:1)

(从我上面的评论中复制)当我使用ubuntu代替windows =时,问题被“解决”了=根问题出现在FileData.java中,其中必须在设置之前调用setContentView(R.layout.file_data)文字到它的TextView(电视)。

FileData.java的新代码是:

package com.capture.ProcessCapture;

... (all the imports)

public class FileData extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File sdcard = Environment.getExternalStorageDirectory();

        Bundle bundle = this.getIntent().getExtras();
        String filename = bundle.getString("filename");
        File file = new File(sdcard+"/ProcessCapture/data", filename);

        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        setContentView(R.layout.file_data);

        TextView tv = (TextView) findViewById(R.id.file_data);
        tv.setText(text);
    }

}

答案 1 :(得分:0)

请查看this question和第一个答案,其中建议在尝试启动活动的代码周围放置一个try / catch块,因为有时实际上并不是该活动确实未声明,而是另一个基础问题

如果这不起作用,您可以随时尝试清理和重建项目,然后让Eclipse重新同步(右键单击项目,“Android工具” - >“>修复项目属性”)。