java Android SDK在发布时挂起设备
大家好,我是初学者,我想编写简单的点击事件,所以我在Android SDK和Eclipse中编写了以下代码:
package com.example.aplic;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
}
}
以及:
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.aplic.MainActivity$PlaceholderFragment" >
<TextView
安卓:id="@+id/textView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/hello_world" />
<Button
安卓:id="@+id/button1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/textView1"
安卓:layout_marginTop="110dp"
安卓:layout_toRightOf="@+id/textView1" />
<ImageView
安卓:id="@+id/imageView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/button1"
安卓:layout_below="@+id/button1"
安卓:layout_marginTop="96dp"
安卓:contentDescription="@string/abc_action_mode_done"
安卓:src="@drawable/abc_ab_bottom_solid_dark_holo" />
</RelativeLayout>
问题是,当我在模拟器上启动代码时,它会挂起并重置,当我在真实设备上启动代码时,它会关闭程序,我不知道它出了什么问题,我是否使用了错误的事件
PS:当我删除onclick事件部分时,它工作正常,但不会在单击时做出反应
主要活动。xml:
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/container"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.deomanapp.MainActivity"
tools:ignore="MergeRootFrame" />
# 1 楼答案
该按钮似乎属于
fragment_main.xml
根据你的评论
它不是挂着的。这是一次撞车。你可能会得到
NullPointerException
改为