简体   繁体   中英

Unfortunately Application has stopped after running it from Android Studio

<LinearLayout 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:orientation="vertical">


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="15dp"
    />
</LinearLayout>   

That is the customized layout xml file I am trying to create.

Here's my code for MainActivity.java:

package com.example.nabil.listviewexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

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

    String [] TVShows = {"Breaking Bad", "Friends", "Sons of Anarchy", "Pushing Daisies",
                            "Life on Mars", "Walking Dead", "Game of Thrones"};

    ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.row_layout ,TVShows);

    ListView listView1 = (ListView) findViewById(R.id.listView1);

    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tvShowPicked = "You Selected " + String.valueOf(parent.getItemAtPosition(position));
            Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
        }
    });
}

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

But every time I try to run in on my galaxy s4, it says Unfortunately Application has stopped. I am still new to this, and I hope it is not a big issue. Thanks!

Screen shots from my logcat: 在此处输入图片说明

在此处输入图片说明

If your XML-Layout file should be the layout of the rows of the list, then as your stacktrace tells you, you are having a LinearLayout where you should only have a TextView. The ArrayAdapter takes only a single TextView as layout as this is the place where it will show your content.

This should be your row_layout.xml:

<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="15dp" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM