This question already has answers here:
NullPointerException accessing views in onCreate()
(13个回答)
5年前关闭。
我刚刚开始开发android应用程序,并在打开以下应用程序时显示
fragmentmain.xml
MainActivity.java
这只是列表视图的简单实现,并且在其元素上实现
(13个回答)
5年前关闭。
我刚刚开始开发android应用程序,并在打开以下应用程序时显示
"Unfortunately your app has crashed"
。任何人都可以指出此代码中的错误。fragmentmain.xml
<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:background="#ffffbb"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="85dp"
android:text="Info of Departments and Student Bodies"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="190dp" >
</ListView>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//adding items to list view
final ListView listview = (ListView) findViewById(R.id.listView);
String values[] = new String[]{"Departments" , "Student_bodies"};
final ArrayList<String> list = new ArrayList<String>();
for (int i=0 ; i < values.length; ++i){
list.add(values[i]);
}
ArrayAdapter<String> ap = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,values);
listview.setAdapter(ap);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (((TextView)arg1).getText().toString() == "Departments"){
startActivity(new Intent(MainActivity.this,Departments.class));
}
if (((TextView)arg1).getText().toString() == "Student_bodies"){
startActivity(new Intent(MainActivity.this,Student_bodies.class));
}
}
});
}
@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();
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 {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
这只是列表视图的简单实现,并且在其元素上实现
onClick()
可以转到不同的Activity
。如果有人指出错误,请多谢。 最佳答案
您的布局文件名为FragmentMain
,但是您使用activity_main
作为setContentView()
方法的参数。尝试更改为setContentView(R.layout.FragmentMain);
10-04 12:31