null指针异常android阳光课1

时间:2015-10-14 17:19:18

标签: android android-studio nullpointerexception

我正在通过udacity android课程学习android。我正在使用android studio。 我陷入了一个我应该看到带有示例数据的列表视图的点,而不是我有NUllPointerException并且应用程序每次都崩溃。

MainActivity.java

    /tmp/ccaLRS7L.o: In function `main':
trial.cpp:(.text+0x26): undefined reference to `Fl_Window::Fl_Window(int, int, char const*)'
trial.cpp:(.text+0x50): undefined reference to `Fl_Box::Fl_Box(int, int, int, int, char const*)'
trial.cpp:(.text+0x5f): undefined reference to `Fl_Window::show()'
trial.cpp:(.text+0x64): undefined reference to `Fl::run()'
trial.cpp:(.text+0x84): undefined reference to `Fl_Window::~Fl_Window()'
trial.cpp:(.text+0xae): undefined reference to `Fl_Window::~Fl_Window()'
/tmp/ccaLRS7L.o: In function `Fl_Box::~Fl_Box()':
trial.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0x13): undefined reference to `vtable for Fl_Box'
trial.cpp:(.text._ZN6Fl_BoxD2Ev[_ZN6Fl_BoxD5Ev]+0x1f): undefined reference to `Fl_Widget::~Fl_Widget()'
collect2: error: ld returned 1 exit status

我的Fragment_main.xml文件:

package com.example.android.sunshine.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
 import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import static com.example.android.sunshine.app.R.id.listView_forecast;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

@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();

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

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    private ArrayAdapter<String> forecastAdapter;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String[] data = {
                "Today-Sunny-88/63",
                "Tomorrow-Sunny-88/63",
                "Weds-Sunny-88/63",
                "Thurs-Sunny-88/63",
                "Fri-Sunny-88/63",
                "Sat-Sunny-88/63",
                "Sun-Sunny-88/63",
        };
        List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
        forecastAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_forecast,
                weekForecast);

        ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
        listView.setAdapter(forecastAdapter);
        return rootView;
    }
}
}

我的list_item_forrecast.xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.sunshine.app.MainActivity$PlaceholderFragment">


<ListView
    android:id="@+id/listView_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

tried this solution, still not working

Logcat screenshot

5 个答案:

答案 0 :(得分:0)

您的适配器如何知道要使用哪个TextView?

将适配器更改为此..

public class ActiveMQConsumer implements MessageListener {

public ActiveMQConsumer() throws JMSException {
    try {
        ConnectionFactory factory = new ActiveMQConnectionFactory(CATALOG_BROKER_URL.getValue());
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic(CATALOG_TOPIC_NAME.getValue());
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(this);
    } catch (JMSException e) {
        System.out.println("Error");
    }
}

@Override
public void onMessage(final Message message) {
    LOGGER.info("Start consuming message from Catalog");
    try {
        if (message instanceof TextMessage) {
            TextMessage txtMessage = (TextMessage) message;
            System.out.println("Message: " + txtMessage.getText());
        } else {
            System.out.println("Invalid Message !");
        }
    } catch (JMSException e) {
        System.out.println("Exception" + e);
    }
}

答案 1 :(得分:0)

尝试使用此简单代码创建ListView。将您的onCreate()方法更改为以下

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


    String[] data = new String[]{"Item1, Item2, Item3, Item4"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_forecast, data);

    ListView lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(adapter);
}

然后删除整个班级PlaceholderFragmentactivity_main布局文件中添加以下代码

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

答案 2 :(得分:0)

根据您为String数据数组粘贴的代码。  String [] data = {                 &#34;今天-阳光-六十三分之八十八&#34 ;,                 &#34;明天-阳光-六十三分之八十八&#34 ;,                 &#34;威尔-阳光-六十三分之八十八&#34 ;,                 &#34;周四-阳光-六十三分之八十八&#34 ;,                 &#34;周五至阳光-六十三分之八十八&#34 ;,                 &#34;周六阳光-六十三分之八十八&#34 ;,                 &#34;太阳阳光-六十三分之八十八&#34 ;,         };

你忘了删除最后一个元素上的逗号。

它应该是这样的

String [] data = {                     &#34;星期一6月23日 - 晴天 - 31/17&#34;,                     &#34;星期二6月24日 - 有雾 - 21/8&#34;,                     &#34;星期三6月25日 - 多云 - 22/17&#34;,                     &#34;周六6月26日 - 雨季 - 18/11&#34;,                     &#34;星期五6月27日 - Foggy - 21/10&#34;,                     &#34;星期六6月28日 - 陷入风化 - 23/18&#34;,                     &#34;太阳6月29日 - 晴天 - 20/7&#34;             }; 我无法在代码中找到任何其他问题,只是尝试纠正数组并重新运行它。 希望能帮助到你.. 欢呼声。

答案 3 :(得分:0)

在Fragment生命周期中,onCreateView()在Fragment访问Activity实例之前的更早阶段被调用。只有在调用onActivityCreated()之后,Fragment才能使用Activity的上下文。

您的问题是您在onCreateView()中使用$('.liclk').click(function(){ $(this).find("li").last().css( "background-color", "red" ); }); 来初始化适配器。将其移至onActivityCreated()并且事情应该没问题。

一个一般性建议:不要在onCreateView()中写太多逻辑。只需充气并返回视图。在onActivityCreated()中写下大部分逻辑,因为那时你肯定知道你有活动的上下文。

答案 4 :(得分:0)

我试过这个如图所示删除该行如果它存在。这可以在DetailActivity.java中找到(在android studio 1.5.1中)

getSupportActionBar()setDisplayHomeAsUpEnabled(真);