不幸的是,从Android Studio运行后,应用程序已停止

时间:2015-06-18 02:52:14

标签: java android 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>   

这是我想要创建的自定义布局xml文件。

这是我的代码 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);
    }
}

但是每当我试图进入我的星系s4时,它说不幸的是应用已停止了。我还是新手,我希望这不是一个大问题。谢谢!

我的logcat屏幕截图:enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你的XML-Layout文件应该是列表行的布局,那么当你的堆栈跟踪告诉你,你有一个LinearLayout,你应该只有一个TextView。 ArrayAdapter只接受一个TextView作为布局,因为它是显示内容的地方。

这应该是你的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" />