运行到setContentView这里崩溃了

这是第一行代码里面的一个项目,运行后崩溃:
图片说明
MainActivity.java

 package com.example.newsapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

News.java

 package com.example.newsapp;

public class News {
    private String title;
    private String content;

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title=title;
    }

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content=content;
    }

}

NewsAdapter.java

 package com.example.newsapp;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class NewsAdapter extends ArrayAdapter<News>
{
    private int resourceId;
    public NewsAdapter(Context context,int textViewResourceId,List<News> objects)
    {
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        News news=getItem(position);
        View view;
        if (convertView==null)
        {
            view=LayoutInflater.from(getContext()).inflate(resourceId,null);
        }
        else
        {
            view=convertView;
        }
        TextView newsTitleText=(TextView)view.findViewById(R.id.news_title);
        newsTitleText.setText(news.getTitle());
        return view;
    }
}

NewsContentActivity.java

 package com.example.newsapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Window;

public class NewsContentActivity extends FragmentActivity
{
    public static void actionStart(Context context,String newsTitle,String newsContent)
    {
        Intent intent=new Intent(context,NewsContentActivity.class );
        intent.putExtra("news_title",newsTitle);
        intent.putExtra("news_content",newsContent);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.news_content);
        FragmentManager fragmentManager=getSupportFragmentManager();
        String newsTitle=getIntent().getStringExtra("news_title");
        String newsContent=getIntent().getStringExtra("news_content");
        NewsContentFragment newsContentFragment=(NewsContentFragment)fragmentManager.findFragmentById(R.id.news_content_fragment);
        newsContentFragment.refresh(newsTitle,newsContent);
    }
}

NewsContentFragment.java

 package com.example.newsapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class NewsContentFragment extends Fragment
{
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.news_content_frag,container,false );
        return view;
    }

    public void refresh(String newsTitle,String newsContent)
    {
        View visibilityLayout=view.findViewById(R.id.visibility_layout);
        visibilityLayout.setVisibility(View.VISIBLE);
        TextView newsTitleText=(TextView)view.findViewById(R.id.news_title);
        TextView newsContentText=(TextView)view.findViewById(R.id.news_content);
        newsTitleText.setText(newsTitle);
        newsContentText.setText(newsContent);
    }
}

NewsTitleFragment.java

 package com.example.newsapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class NewsTitleFragment extends Fragment implements OnItemClickListener
{
    private ListView newsTitleListView;
    private List<News> newsList;
    private NewsAdapter adapter;
    private boolean isTwoPane;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        newsList=getNews();
        adapter=new NewsAdapter(activity,R.layout.news_item,newsList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
        View view=inflater.inflate(R.layout.news_title_frag,container,false );
        newsTitleListView=(ListView)view.findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this );
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        if (getActivity().findViewById(R.id.news_content_layout)!=null )
        isTwoPane=true;
        else
        isTwoPane=false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent,View view,int position,long id)
    {
        News news=newsList.get(position);
        if (isTwoPane)
        {
            NewsContentFragment newsContentFragment=(NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
            newsContentFragment.refresh(news.getTitle(),news.getContent());
        }
        else
        {
            NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
        }
    }

    private List<News> getNews()
    {
        List<News> newsList=new ArrayList<News>();
        News news1=new News();
        news1.setTitle("标题");
        news1.setContent("内容");
        newsList.add(news1);
        News news2=new  News();
        news2.setTitle("日记");
        news2.setContent("去旅游");
        newsList.add(news2);
        return newsList;
    }

}

5个回答

检查下AndroidManifest.xml第11行。

YXTS122
YXTS122 public class MainActivity extends FragmentActivity这个继承是FragmentActivity,不是Activity
大约 3 年之前 回复
YXTS122
YXTS122 知道了,我没有为NewsContentActivity在AndroidManifest中注册
大约 3 年之前 回复
YXTS122
YXTS122 我截图了,你看下
大约 3 年之前 回复

图片说明
图片说明
图片说明
图片说明
图片说明
图片说明

图片说明

修改了一下代码,界面出来了,但点击一下崩溃了:
图片说明

要是用android.support.v4.app.Fragment的话,是不能是用标签的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问