我有一个应用程序,它有一个计数器并将其存储到数据库中。我试图通过制作视图布局类和视图按钮来检查查询语句是否正常工作。但是当我点击视图按钮时,它会强制关闭并且日志猫说没有活动可以找到。 我检查确保它在清单中,并确保它拼写和大写正确。
package com.example.testdatabase;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add;
Button sub;
Button save;
Button view;
TextView display;
private SharedPreferences prefs;
private String prefName = "MyPref";
MySQLiteHelper db = new MySQLiteHelper(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
save = (Button) findViewById(R.id.button3);
view = (Button) findViewById(R.id.button4);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Counter: " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Counter: " + counter);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.add(counter);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.example.testdatabase.Sqlview");
startActivity(i);
}
});
}
protected void onPause()
{
super.onPause();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("counter", counter);
edit.commit();
}
protected void onResume()
{
super.onResume();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
counter = prefs.getInt("counter", counter);
display.setText("Counter: " + counter);
}
}
查看课程
package com.example.testdatabase;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Sqlview extends Activity {
protected void onCreate(Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.textView1);
MySQLiteHelper info = new MySQLiteHelper(this);
String data = info.getData();
tv.setText(data);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdatabase"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testdatabase.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.testdatabase.Sqlview"
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>
logcat的
11-17 17:29:15.394: E/AndroidRuntime(279): FATAL EXCEPTION: main
11-17 17:29:15.394: E/AndroidRuntime(279): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.testdatabase.Sqlview }
11-17 17:29:15.394: E/AndroidRuntime(279): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.app.Activity.startActivityForResult(Activity.java:2817)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.app.Activity.startActivity(Activity.java:2923)
11-17 17:29:15.394: E/AndroidRuntime(279): at com.example.testdatabase.MainActivity$4.onClick(MainActivity.java:71)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.view.View.performClick(View.java:2408)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.view.View$PerformClick.run(View.java:8816)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.os.Handler.handleCallback(Handler.java:587)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
11-17 17:29:15.394: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-17 17:29:15.394: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 17:29:15.394: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
11-17 17:29:15.394: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-17 17:29:15.394: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-17 17:29:15.394: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你的Intent使用了错误的构造函数。它应该是:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Sqlview.class);
startActivity(i);
}
});