我正在開發應用程序,我一直在研究「發佈」服務,您可以在其中撰寫帖子並將其顯示在由listview製作的提要屏幕上。 現在一切似乎都很好,但我不知道爲什麼它甚至在開始之前崩潰。 下面是代碼:應用程序在打開之前崩潰
Post類:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Post extends ActionBarActivity {
EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="@+id/input"
input = (EditText) findViewById(R.id.input);
// android:id="@+id/list"
startActivityForResult(new Intent(Post.this , MainActivity.class), 1);
}
public void addToList(View v) {
Editable text = input.getText();
Intent intent = new Intent();
intent.putExtra("result", text);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
在這個類我有一個編輯文字和A按鈕,一旦按下按鈕,我應該得到在編輯文本和後文其他類的ListView
主營:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView list;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
}
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();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id2 == R.id.action_cart) {
startActivity(new Intent(MainActivity.this, Post.class));
}
return super.onOptionsItemSelected(item);
}
}
和XML文件是:
帖子:
<RelativeLayout 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.ali.test1.Post">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="@id/input"/>
</RelativeLayout>
主營:
<RelativeLayout 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.ali.test1.MainActivity">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
任何幫助,將不勝感激,我一直呆在這裏太久。 謝謝。
請過程中,附上堆棧跟蹤 –
和你的logcat – Sree
'ListView'尚未初始化是'null' –