我正在使用Android工作室进行Android开发。我正在创建onclick按钮活动。我没有错误但是当我点击应用程序上的按钮时它显示“不幸的是应用程序已停止”。 下面是我的fragment_main.xml和MainActivity.java的代码。 请指导。 fragment_main.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=".MainActivity$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/texthaiku"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/editText"
android:onClick="OnLoveButtonClicked"/>
<TextView
android:text="@string/haiku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_below="@+id/button"
android:visibility="invisible"
android:layout_alignParentLeft="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
MainActivity.java
package com.AndroidLove;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onLoveButtonClicked(View view) {
TextView textView =(TextView) findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
}
@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.
switch (item.getItemId()) {
case 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;
}
}
}
答案 0 :(得分:1)
试试这个..
它会给NPE,因为 textView 位于fragment_main.xml
内,然后您在单击函数内部启动该textview,这是错误的。在全局
TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
textView =(TextView) rootView.findViewById(R.id.textView);
return rootView;
}
和
public void onLoveButtonClicked(View view) {
textView.setVisibility(View.VISIBLE);
}
答案 1 :(得分:0)
您尚未在TextView
onCreateView()
所以在onCreateView()
textView =(TextView) rootView.findViewById(R.id.textView);
修改强>
也初始化你的按钮。
btn = (Button) rootView.findViewById(R.id.button);
答案 2 :(得分:0)
你在哪里创建了容器,在你显示的xml中我看不到任何容器布局。 没有R.id.container