gpt4 book ai didi

android - (Android) 无法从同一包中的另一个 Activity 调用一个 Activity

转载 作者:太空狗 更新时间:2023-10-29 14:29:53 26 4
gpt4 key购买 nike

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

list 文件:

<?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();
}

}

文件数据.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)

提前致谢!很抱歉问了这么长的问题 =)

最佳答案

(从我上面的评论中复制)当我使用 ubuntu 而不是 windows 时,问题已“解决”=/根本问题在 FileData.java 中,在设置之前必须调用 setContentView(R.layout.file_data)文本到它的 TextView (tv)。

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);
}

}

关于android - (Android) 无法从同一包中的另一个 Activity 调用一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8019744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com