切换活动时Android应用程序崩溃

时间:2013-04-18 00:41:12

标签: android

点击列表视图中的任何项目后,我想要启动一项新活动。当我通过模拟器运行时,我的应用程序每次都会崩溃。我很肯定我的AndroidManifest也是正确的。任何帮助赞赏。

public static class SectionFragment extends ListFragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    private String dataArrayOne[];
    private String dataArrayTwo[];

    public SectionFragment() {
        dataArrayOne = new String[] {
                "Steven's Portfolio",
                "Sean's Portfolio",
                "Logan's Portfolio",
        };

        dataArrayTwo = new String[] {
                "GOOG",
                "YHOO",
                "AAPL",
                "MSFT"
        };
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        switch(getArguments().getInt(ARG_SECTION_NUMBER))
        {
        case 0:
            ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayOne);
            setListAdapter(listAdapter);
            break;
        case 1:
            ListAdapter listAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dataArrayTwo);
            setListAdapter(listAdapter1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_dummy, container, false);
    }

    @Override
    public void onListItemClick(ListView list, View v, int position, long id)
    {
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(getActivity(), Portfolio_Select.class);
        intent.putExtra("USERNAME", getListView().getItemAtPosition(position).toString());
        startActivity(intent);

    }

的AndroidManifest.xml

<activity
        android:name=".Portfolio_Select"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

Portfolio_Select Code

public class Portfolio_Select extends Activity {

    // variables go here

    TextView display;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.stockticker.R.layout.activity_main);



        display.setText("Stocks go here!");


    }


}

1 个答案:

答案 0 :(得分:2)

TextView display;

已声明,但它始终引用null,因为findViewById()尚未用于为其提供任何其他内容。

这意味着什么时候

display.setText("Stocks go here!");

被叫,你会得到一个NPE。

因此,请确保TextView中的R.layout.activity_main有一个ID,然后使用findViewById()display提供TextView参考

setContentView(com.example.stockticker.R.layout.activity_main);

display = (TextView) findViewById (R.id.your_text_view_id);

display.setText("Stocks go here!");

如果还有其他问题,请将堆栈跟踪添加到您的问题中,因此不会涉及其他猜测。