gpt4 book ai didi

java - 无法找到或启动 Activity Intent

转载 作者:行者123 更新时间:2023-12-01 13:50:03 24 4
gpt4 key购买 nike

我有一个应用程序,它有一个计数器并将其存储到数据库中。我试图通过创建 View 布局类和 View 按钮来检查查询语句是否正常工作。但是,当我单击“查看”按钮时,它强制关闭,并且日志猫说没有找到任何 Activity 。我检查以确保它在 list 中,并确保它的拼写和大写正确。

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



}

}

list

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

日志猫

    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)

最佳答案

您为 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);

}
});

关于java - 无法找到或启动 Activity Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037196/

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