我是android的新手...在视频教程中我看着编写代码并编译它的人没有任何错误..但是当我键入的相同代码给我错误..不知道为什么。 。 嗯..布局xml文件没问题..在java文件中有一个错误..每当我将onclicklistener方法添加到一个按钮时...它在代码部分没有给出任何错误但是当我编译它时它显示一个错误..应用程序XXX意外停止。请再试一次。
代码就在这里..
package com.abrosoft.trycommand;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
EditText input;
TextView display;
@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();
}
Button cmd = (Button) findViewById(R.id.bCmd);
display = (TextView) findViewById(R.id.tvcmd);
input = (EditText) findViewById(R.id.etinput);
//if I delete this SetOnClickListener method.. it runs ok..
//I get xml layout on screen of emulator.. but with this method..it doesnot run
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//even I write any lines of code which should perform any action on button
//click is giving same error
}
});
}
@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;
}
}
}
这是我的xml文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="25dp"
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"
tools:context="com.abrosoft.trycommand.MainActivity$PlaceholderFragment" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type a command"
android:id="@+id/etinput"
android:inputType="text"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bCmd"
android:layout_weight="50"
android:text="Command"
/>
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tgB"
android:layout_weight="50"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/tvcmd"
/>
提前致谢
答案 0 :(得分:1)
删除条件
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
同时删除PlaceHolder类
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;
}
并将setContentView()
的{{1}}方法更改为
OnCreate(Bundle savedInstances)
答案 1 :(得分:0)
只需在代码中评论以下部分,然后尝试运行。
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
答案 2 :(得分:0)
删除此
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
来自onCreate(Bundle)方法的。 它会有所帮助
答案 3 :(得分:0)
将以下代码放在onCreateView
Button cmd = (Button) findViewById(R.id.bCmd);
display = (TextView) findViewById(R.id.tvcmd);
input = (EditText) findViewById(R.id.etinput);
//if I delete this SetOnClickListener method.. it runs ok..
//I get xml layout on screen of emulator.. but with this method..it doesnot run
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//even I write any lines of code which should perform any action on button
//click is giving same error
}
});
将onCreateView
更改为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button cmd = (Button) rootView.findViewById(R.id.bCmd);
display = (TextView) rootView.findViewById(R.id.tvcmd);
input = (EditText) rootView.findViewById(R.id.etinput);
//if I delete this SetOnClickListener method.. it runs ok..
//I get xml layout on screen of emulator.. but with this method..it doesnot run
cmd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//even I write any lines of code which should perform any action on button
//click is giving same error
}
});
return rootView;
}