Android:无法从内部类启动新的 Activity

标签 android android-intent android-activity

Android:有人帮忙: 我注意到以前其他人也问过这种问题,但答案对我的情况没有用;我需要从内部发起一项新 Activity 类,但我得到的只是下面的错误:

04-05 15:00:43.851: E/AndroidRuntime(3288): Caused by: java.lang.InstantiationException: com.school.School$StudentProfile

这是我的代码 fragment :

public class School extends Activity{
ProgressDialogue progressDialogue;

protected WebViewTask _webTask;

String path = "http://www.school.com/student/"; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.school);


    progressDialogue = new ProgressDialogue(School.this);

    _webTask = new WebViewTask();
    _webTask.execute();

}   

//rest of the code

  /** The inner class */

public class StudentProfile {
    Context context;


    /** Instantiate the interface and set the context */

    public StudentProfile(Context c) {
         context=c;
        }

    /** launch student activity */
    public void lauchProfile() {

         School.this.startActivity(new Intent(School.this, StudentProfile.class));
        //Intent intent = new Intent(School.this, StudentProfile.class);

        //startActivity(intent);

    }
}   

void webView(){
    String url = path +"student.php";   

    WebView wv = (WebView) findViewById(R.id.trivia_webview);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);

    wv.addJavascriptInterface(new StudentProfile (this), "Student");

    wv.loadUrl(url);

    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // open URL in the web view itself
            if (url.contains(url))
                return super.shouldOverrideUrlLoading(view, url);
            // open URL in an external web browser
            else {

                return true;
            }
        }
    });
}

// rest of the code

注意:Web View 上有一个“学生”按钮,用于启动 StudentProfile Activity 。

最佳答案

您的StudentProfile 不是Activity,因此您不能以这种方式启动它。它需要是一个单独的类,并在 AndroidManifest.xml 中声明。

关于Android:无法从内部类启动新的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846968/

相关文章:

Android:将SQL数据放入数组

java - 如何更改抽屉导航部分标题的颜色

android - 扩展 Gallery 的 CoverFlow(已弃用,在 >4.0 设备中无法正常工作)

android - 具有相同 Intent 的推送通知打开 Activity

android - 在android中的 fragment 之间传递数据数组

缺少 Android Intent 参数 FLAG_ACTIVITY_SINGLE_TASK?

java - Class.forName() 不断显示错误 : java. lang.NullPointerException

android - 是否可以将 Activity 和 fragment 与底部导航一起使用?

java - 停止 fragment 的函数和调用并重置内部变量

屏幕旋转后Android listview消失