Android 当我按下工具栏中的后退箭头时,会得到一个空对象引用

Android 当我按下工具栏中的后退箭头时,会得到一个空对象引用,android,android-toolbar,Android,Android Toolbar,我正在尝试将数据从第二个活动发送到第三个活动,以确保其正常工作 好的,当我点击工具返回中的返回箭头时,我得到了这个错误,请任何人帮助我解决这个问题 这是我的第二个活动代码 pos=Integer.parseInt((getIntent().getExtras()).getString("pos")); webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl(); webviewurl2=News

我正在尝试将数据从第二个活动发送到第三个活动,以确保其正常工作 好的,当我点击工具返回中的返回箭头时,我得到了这个错误,请任何人帮助我解决这个问题

这是我的第二个活动代码

pos=Integer.parseInt((getIntent().getExtras()).getString("pos"));


    webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();

    webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();

news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
news_site_like_two=   (TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);

  news_site_link_one.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
       webviewintent.putExtra("webviewurl", webviewurl);
         startActivity(webviewintent);

    }
   });
下面是我的网络视图

 String SourceURL;
WebView webview;
final Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);




    setContentView(R.layout.activity_news__web_view);
    Intent intent =getIntent();


        SourceURL =  intent.getStringExtra("webviewurl");
    webview=(WebView)findViewById(R.id.webView);
    webview.getSettings().setJavaScriptEnabled(true);




    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webview.loadUrl(SourceURL);
}
@Override
public void onBackPressed() {
    if (webview.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
        overridePendingTransition(R.transition.slide_in_down, R.transition.slide_out_down);

    }
}
下面是我的错误代码

Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

这意味着这一行:

pos=Integer.parseInt((getIntent().getExtras()).getString("pos"));
指向nothing,getString(“pos”)不存在

这可能有很多原因。当您在工具栏中按back键时,可能您没有用额外的内容填充意图

您可以执行空检查:

if(getIntent().getExtras() != null)
   pos=Integer.parseInt((getIntent().getExtras()).getString("pos"));
但是最好在你的工具栏中处理它,返回ClickListener并确保它不是空的

顺便说一下:

Integer.parseInt((getIntent().getExtras()).getString("pos"))   
这不是好的做法。如果要从目标中获取单个整数,请使用:

getIntent().getIntExtra("pos", 0); // will be 0 if null
你把它放在意向书中,就像这样:

intent.putExtra("pos", 666);

现在,您的意向额外“pos”被分配值666。

这意味着此行:

pos=Integer.parseInt((getIntent().getExtras()).getString("pos"));
指向nothing,getString(“pos”)不存在

这可能有很多原因。当您在工具栏中按back键时,可能您没有用额外的内容填充意图

您可以执行空检查:

if(getIntent().getExtras() != null)
   pos=Integer.parseInt((getIntent().getExtras()).getString("pos"));
但是最好在你的工具栏中处理它,返回ClickListener并确保它不是空的

顺便说一下:

Integer.parseInt((getIntent().getExtras()).getString("pos"))   
这不是好的做法。如果要从目标中获取单个整数,请使用:

getIntent().getIntExtra("pos", 0); // will be 0 if null
你把它放在意向书中,就像这样:

intent.putExtra("pos", 666);
现在,您的intent Extra“pos”被赋值为666。

根据,getExtras():

返回以前使用putExtra()添加的所有额外项的映射,,如果未添加任何额外项,则返回null

看起来您从未调用putExtra()或未正确使用它

根据,getExtras():

返回以前使用putExtra()添加的所有额外项的映射,,如果未添加任何额外项,则返回null


看起来您从未调用putExtra()或未正确使用它

看起来错误在下面的行中

pos=Integer.parseInt((getIntent().getExtras()).getString(“pos”)

所以你可以在这里做两件事


方法1


方法2 改变你的背压方法

@Override
public void onBackPressed() 
{
  if (webview.canGoBack())
  {
     webview.goBack();
  } else {
          Intent backIntent = new Intent(getApplicationContext(),  PreviousClass.class);       
     backIntent.putExtra("pos",pos);
     startActivity(webviewintent);
    overridePendingTransition(R.transition.slide_in_down,  R.transition.slide_out_down);
   }
}


但在从第二个活动移动到第三个活动时,您也需要传递pos变量。

下面的行中似乎有错误

pos=Integer.parseInt((getIntent().getExtras()).getString(“pos”)

所以你可以在这里做两件事


方法1


方法2 改变你的背压方法

@Override
public void onBackPressed() 
{
  if (webview.canGoBack())
  {
     webview.goBack();
  } else {
          Intent backIntent = new Intent(getApplicationContext(),  PreviousClass.class);       
     backIntent.putExtra("pos",pos);
     startActivity(webviewintent);
    overridePendingTransition(R.transition.slide_in_down,  R.transition.slide_out_down);
   }
}


但是在从第二个活动移动到第三个活动时,也需要传递pos变量。

此错误指的是代码中的哪一行?pos=Integer.parseInt((getIntent().getExtras()).getString(“pos”)@你们知道空指针异常是什么吗?实际上,当我在这个工具中单击back Arrow时,代码正在工作并得到结果error@AHoneyBustardWhat你的意思是“在工具中”?这个错误指的是代码中的哪一行?pos=Integer.parseInt((getIntent().getExtras()).getString(“pos”)@你们知道空指针异常是什么吗?实际上,当我在这个工具中单击back Arrow时,代码正在工作并得到结果error@AHoneyBustardWhat你是说“在工具里”吗?