java - WebView loadDataWithbaseURL java.lang.NullPointerException

标签 java android webview

//. . .

public class MainActivity extends Activity {

    //. . .

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        //. . .
        mainActivityProcedure ( );
    }

    //. . .

    public static void mainActivityProcedure ( ) {
        //. . .
        AnotherActivity.anotherActivityProcedure ( ) ;
        SomeActivity.someActivityProcedure ( );
        //. . .
    }

    //. . .

}

//. . .

public class SomeActivity extends Activity {

    //. . .

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        //. . .
        someWebView = ( WebView ) findViewById ( R.id.someWebView );
        //. . .
    }

    //. . .

    public static void someActivityProcedure ( ) {
        //. . .
        try {
            someWebView.loadDataWithBaseURL ( null , someHTMLCode, mimeType, encoding, null );
    catch ( Exception e ) {
        Log.i("SomeActivity", "someWebView loadDataBaseURL () " + e.toString() );
    }
        //. . .
    }

    //. . .
}

//. . .

public class AnotherActivity extends Activity {

    //. . .

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        //. . .
        anotherWebView = ( WebView ) findViewById ( R . id.anotherWebView ) ;
        //. . .
    }

    //. . .

    public static void anotherActivityProcedure ( ) {
        //. . .
        anotherWebView . loadDataWithBaseURL ( null, anotherHTMLCode, mimeType, encoding, null ) ;
        //. . .
    }
}

当 anotherActivityProcedure 调用时,AnotherActivity 总是加载 anotherHTMLCode。 SomeActivity 加载 someHTMLCode,除非 mainActivityProcedure 从 MainActivity.onCreate 调用。 someWebView 或 SomeActivity 有什么问题? 我不能在这里放置原始代码。

添加:loadDataWithBaseURL() 中的 Replacin` null 无法修复它。 添加:Logcat 显示消息 java.lang.NullPointerException,该消息将 Log.i() 从 catch 中取出。

最佳答案

试试这个,在 MainActivity 中初始化 SomeActivity 并使用您可以访问的 _someactivity 对象。

SomeActivity _someactivity = new SomeActivity();
_someactivity.someActivityProcedure ();

关于java - WebView loadDataWithbaseURL java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331298/

相关文章:

android - 如何使用相机作为 Facebook Android Webview 中文件输入的来源

java - 为什么我会收到此 NoSuchProviderException

java - 在类中添加 Intent

java - 在字符串第 n 次出现后追加字符串

java - 在Spring应用程序中设置查询超时连接时出现问题

java - 根据微调器选择显示 ListView

java - Activemq Topic Consumer 消费某些消息失败

java - 带有 NavigationDrawer 的 fragment 中的 TabHost

android - RecyclerView 内的 WebView,滚动问题

android - 使用 webview 显示地理位置