質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

941閲覧

listviewが表示されない

makioo

総合スコア28

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2018/08/27 18:07

編集2018/08/27 21:33

javaソース

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button newCreate; private ListView listitem; private List<DtoItem> items; private static final String[] texts = { "abc ", "bcd", "cde", "def", "efg", "fgh" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); newCreate = (Button)findViewById(R.id.button); newCreate.setOnClickListener(this); items = DaoItem.findAll(getApplicationContext()); // ListView listView = new ListView(this); // setContentView(listitem); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, texts); listitem.setAdapter(arrayAdapter);

xmlソース

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.minamigani.todolist.MainActivity" tools:layout_editor_absoluteY="81dp" android:id="@+id/constrainlayout"> <ListView android:id="@+id/todolistview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" tools:layout_editor_absoluteY="100dp" tools:layout_editor_absoluteX="60dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:visibility="visible" tools:layout_editor_absoluteY="464dp" tools:layout_editor_absoluteX="0dp"> <Button android:text="@string/NewCreate" android:layout_width="wrap_content" android:layout_height="47dp" android:id="@+id/button" app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.895" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.034"/> </LinearLayout> </android.support.constraint.ConstraintLayout>

実行すると

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

と言われました。何が違うのでしょうか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

初歩的なミス

listitem = (ListView)findViewById(R.id.todolistview);

viewの設定をしていなかった。。。

投稿2018/08/27 21:34

makioo

総合スコア28

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問