Android Studio - Udacity Lesson1 33-39,无法正常工作

时间:2015-09-18 06:42:32

标签: java android android-fragments android-activity android-studio

所以我对Udacity教程有问题Here is the lesson 我按照说明做了所有事情,但是我遇到了错误,我不知道如何解决。

这是我的

MainActivity.java

package com.sunshine.elksomik.sunshine;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

和我的

MainActivityFragment

package com.sunshine.elksomik.sunshine;

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

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
        String[] forecastArray = {
                "Today - Sunny 88/63",
                "Tomorrow - Foggy = 70/40"
        };
        List<String> weekForecast = new ArrayList<String>(
                Arrays.asList(forecastArray)
        );
        mForcastAdapter =
                new ArrayAdapter<String>(
                        getActivity(),
                        R.layout.list_item_forcast,
                        R.id.list_item_forcast_textview,
                        weekForecast);
        ListView listView = (ListView) rootView.findViewById(
                listView.setAdapter(mForacastAdapter);
        );
    }
}

例如在教程中,ActivityFragment类或函数位于MainActivity的一个文件中,但在我的文件中它们是分开的,并且rootView甚至不存在,无法找到它。

1 个答案:

答案 0 :(得分:0)

将onCreateView()更改为此类

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container, false);
          /* write your function 
                   inside here */
   return rootView;
    }