Android Studio:应用已停止但没有错误/ OnClickListener问题

时间:2017-03-23 17:25:18

标签: android onclicklistener

如图所示,我想要发生的是,如果我按下Dwyane Wade2,它应该重定向到显示有关Dwyane Wade2的信息的页面。但是,如果我这样做,它会提示“应用程序已停止”并重定向回主屏幕。没有显示错误,所以我不确定它出错的地方。

screen

这是相关部分。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_content);

        Intent receiveIntent = getIntent();
        String headline = receiveIntent.getStringExtra("HEADLINE");

        TextView txv_author = (TextView) findViewById(R.id.txv_author);
        txv_author.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView target_author = (TextView) findViewById(R.id.txv_author);
                        Intent launchIntent = new Intent(ArticleContentActivity.this, AuthorActivity.class);
                        launchIntent.putExtra("AUTHOR", target_author.getText());
                        startActivity(launchIntent);
                        return;
                    }
                }

        );

        TextView txv_publisher = (TextView) findViewById(R.id.txv_publisher);
        txv_publisher.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView target_publisher = (TextView) findViewById(R.id.txv_author);
                        Intent launchIntent = new Intent(ArticleContentActivity.this, PublisherActivity.class);
                        launchIntent.putExtra("PUBLISHER", target_publisher.getText());
                        startActivity(launchIntent);
                        return;
                    }
                }

        );

        ArtickelApp app = (ArtickelApp) getApplication();
        ArrayList<article> articleList = app.getArticleList();

        article selectedArticle = null;

        for (int i = 0;i < articleList.size(); i++) {
            String tempHeadline = articleList.get(i).getHeadline();

            if (headline.equals(tempHeadline) == true) {
                selectedArticle = articleList.get(i);
                break;
            }
        }

        if (selectedArticle != null)
        {
            loadarticleProfile(selectedArticle);
        }
        return;
    }

    private void loadarticleProfile(article articleProfile)
    {
        TextView txv_Headline = (TextView) findViewById(txv_headline);
        TextView txvDet1 = (TextView) findViewById(R.id.txv_author);
        TextView txvDet2 = (TextView) findViewById(R.id.txv_content);
        TextView txvDet3 = (TextView) findViewById(R.id.txv_publisher);

        txv_Headline.setText (articleProfile.getHeadline());
        txvDet1.setText (articleProfile.getAuthor() );
        txvDet2.setText (articleProfile.getContent() );
        txvDet3.setText (articleProfile.getPublisher());
      //  txvDet3.setText (friendProfile.getDescription());

        return;
    }
    }

作者活动页面:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author);

        Intent receiveIntent = getIntent();
        final String author = receiveIntent.getStringExtra("AUTHOR");

        ArtickelApp app = (ArtickelApp) getApplication();
        ArrayList<Author> authorList = app.getAuthorList();

        Author selectedAuthor = null;

        for (int i = 0;i < authorList.size(); i++) {
            String tempAuthor = authorList.get(i).getAuthor();

            if (author.equals(tempAuthor) == true) {
                selectedAuthor = authorList.get(i);
                break;
    }
}
        if (selectedAuthor != null)
        {
            loadProfile(selectedAuthor);
        }
        return;
    }

    private void loadProfile(Author authorProfile)
    {
        TextView txvName = (TextView) findViewById(R.id.txv_authorname);
        TextView txvDet2 = (TextView) findViewById(R.id.txv_authorinfo);

        txvName.setText (authorProfile.getAuthor());
        txvDet2.setText (authorProfile.getInfo());
    }
}

2 个答案:

答案 0 :(得分:1)

target_publisher移出target_authorTextView onClick初始化:

请按照以下代码:

TextView target_author;
TextView target_publisher;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_content);

        Intent receiveIntent = getIntent();
        String headline = receiveIntent.getStringExtra("HEADLINE");

        TextView txv_author = (TextView) findViewById(R.id.txv_author);
        target_author = (TextView) findViewById(R.id.txv_author);
        txv_author.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent launchIntent = new Intent(ArticleContentActivity.this, AuthorActivity.class);
                        launchIntent.putExtra("AUTHOR", target_author.getText());
                        startActivity(launchIntent);
                    }
                }

        );

        TextView txv_publisher = (TextView) findViewById(R.id.txv_publisher);
        target_publisher = (TextView) findViewById(R.id.txv_author);
        txv_publisher.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent launchIntent = new Intent(ArticleContentActivity.this, PublisherActivity.class);
                        launchIntent.putExtra("PUBLISHER", target_publisher.getText());
                        startActivity(launchIntent);
                    }
                }

        );

希望这有帮助。

答案 1 :(得分:0)

查看您的Android监视器,看看logcat中显示的异常。 我敢打赌它在任何时候都是{{1}}。某些变量/参数为null,您尝试访问它。

而且,更一般的看:你不需要{{1}}语句作为方法的最后一个语句:)它无论如何都会返回。