我创建了一个打开另一个激活的按钮。当我运行我的应用程序时,它崩溃并说“不幸的是程序停止了”。可能有人发现错误。这是我的代码:
MainActivty:
package com.example.listview2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button but = (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivity(intent);
}
});
}
}
ListActivity:
package com.example.listview2;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ListActivity extends Activity {
private EditText etInput;
private Button btnAdd;
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
addItemList();
}
return true;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(etInput)) {
itemArrey.add(0,etInput.getText().toString());
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listview2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.listview2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.listview2.ListActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
logcat的:
06-09 12:30:00.311: E/AndroidRuntime(2012): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 12:30:00.311: E/AndroidRuntime(2012): at android.os.Looper.loop(Looper.java:137)
06-09 12:30:00.311: E/AndroidRuntime(2012): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-09 12:30:00.311: E/AndroidRuntime(2012): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 12:30:00.311: E/AndroidRuntime(2012): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 12:30:00.311: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-09 12:30:00.311: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-09 12:30:00.311: E/AndroidRuntime(2012): at dalvik.system.NativeStart.main(Native Method)
06-09 12:30:00.311: E/AndroidRuntime(2012): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.listview.FirstActivity" on path: /data/app/com.example.listview-2.apk
06-09 12:30:00.311: E/AndroidRuntime(2012): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
06-09 12:30:00.311: E/AndroidRuntime(2012): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-09 12:30:00.311: E/AndroidRuntime(2012): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-09 12:30:00.311: E/AndroidRuntime(2012): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
06-09 12:30:00.311: E/AndroidRuntime(2012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
答案 0 :(得分:0)
系统找不到您正在呼叫的FirstActivity
活动。您需要在manifest.xml
在清单中添加以下内容:
<activity android:name="com.example.listview.FirstActivity" />
答案 1 :(得分:0)
清理并构建然后再次运行项目。你可以将ListActivity重命名为其他东西,就像blackbelt所说的那样。如果这不起作用,那么在Intent中给出完整的包名,然后就像
一样Intent intent = new Intent(MainActivity.this, com.example.listview2.ListActivity.class);