我起诉Android Studio 1.5.1
android api 23
。
当我单击计算按钮时,它不会触发public void onClick(View v)
功能。
以下是MainActivity.java
:
package gradescalculator.com.calculator.gradescalculator;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText answer1;
EditText answer2;
EditText answer3;
private View calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// calculate = () findViewById(R.id.calculate);
calculate = findViewById(R.id.calculate);
calculate.setOnClickListener(this);
/*
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
answer1 = (EditText) findViewById(R.id.answer1);
answer2 = (EditText) findViewById(R.id.answer2);
answer3 = (EditText) findViewById(R.id.answer3);
String answer1Value = answer1.getText().toString();
String answer2Value = answer2.getText().toString();
String answer3Value = answer3.getText().toString();
Float answer1ValueFloat = new Float(answer1Value);
Float answer2ValueFloat = new Float(answer2Value);
Float answer3ValueFloat = new Float(answer3Value);
Float average = (answer1ValueFloat+answer2ValueFloat+answer3ValueFloat )/3;
Intent i = AnswerActivity.newIntent(MainActivity.this,average);
startActivityForResult(i,0);
}
});
*/
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
new AlertDialog.Builder(this)
.setTitle(R.string.alert_title)
.setItems(R.array.languages,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
if (i == 1) {
setContentView(R.layout.activity_main);
} else {
setContentView(R.layout.activity_main_fr);
}
}
})
.show();
}
@Override
public void onClick(View v) {
answer1 = (EditText) findViewById(R.id.answer1);
answer2 = (EditText) findViewById(R.id.answer2);
answer3 = (EditText) findViewById(R.id.answer3);
String answer1Value = answer1.getText().toString();
String answer2Value = answer2.getText().toString();
String answer3Value = answer3.getText().toString();
Float answer1ValueFloat = new Float(answer1Value);
Float answer2ValueFloat = new Float(answer2Value);
Float answer3ValueFloat = new Float(answer3Value);
Float average = (answer1ValueFloat+answer2ValueFloat+answer3ValueFloat )/3;
Intent i = AnswerActivity.newIntent(MainActivity.this,average);
startActivityForResult(i,0);
}
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以下是AnswerActivity.java
:
package gradescalculator.com.calculator.gradescalculator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by aa on 3/6/2016.
*/
public class AnswerActivity extends AppCompatActivity {
private static final String CALCULATE_ANSWER="com.calculate.answer";
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_answer);
}
public static Intent newIntent(Context packageContext, Float average) {
Intent i = new Intent(packageContext, AnswerActivity.class);
i.putExtra(CALCULATE_ANSWER, average);
return i;
}
}
以下是activity_answer.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="gradescalculator.com.calculator.gradescalculator.MainActivity"
tools:showIn="@layout/activity_main">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grades_average"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:id="@+id/answerGrade1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade1"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:id="@+id/answerGrade2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade2"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:id="@+id/answerGrade3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade3"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grades_average"/>
<TextView
android:id="@+id/gradeAverage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
以下是activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="gradescalculator.com.calculator.gradescalculator.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
以下是content_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="gradescalculator.com.calculator.gradescalculator.MainActivity"
tools:showIn="@layout/activity_main">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enter_grades"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade1"/>
<EditText
android:id="@+id/answer1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade2"/>
<EditText
android:id="@+id/answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grade3"/>
<EditText
android:id="@+id/answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip">
<Button android:id="@+id/calculate"
android:text="@string/calculate_grade" />
</TableRow>
</TableLayout>
</RelativeLayout>
答案 0 :(得分:1)
首先将MainActivity.java中'calculate'的视图类型从“View”更改为“Button”。您需要将计算类型转换为Button视图以将OnClickListener设置为它。基本上,替换
private View calculate;
与
private Button calculate;
和
calculate = findViewById(R.id.calculate);
用,
calculate = (Button) findViewById(R.id.calculate);
答案 1 :(得分:0)
Calculate是Button的id,所以你应该在java类中初始化Button而不是view。像这样:
//reload partially page with ajax (only new content between <info> tags)
var HTTP = {};
HTTP._factories = [function () {
return new XMLHttpRequest();
}, function () {
return new ActiveXObject("Msxml2.XMLHTTP");
}, function () {
return new ActiveXObject("Microsoft.XMLHTTP");
}
];
HTTP._factory = null;
HTTP.newRequest = function () {
if (HTTP._factory != null)
return HTTP._factory();
for (var i = 0; i < HTTP._factories.length; i++) {
try {
var factory = HTTP._factories[i];
var request = factory();
if (request != null) {
HTTP._factory = factory;
return request;
}
} catch (e) {
continue;
}
}
HTTP._factory = function () {
throw new Error("This browser doesn't permit automatic reload of content");
}
HTTP._factory();
}
//reloads contents inside of <info> tags every minute
function reloadInfo() {
var request = HTTP.newRequest();
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200)
var infodoc = request.responseText;
var infodiv = infodoc.substring(infodoc.indexOf('<info>'), infodoc.indexOf('</info>') + 7);
document.getElementById('info').parentNode.innerHTML = infodiv;
}
}
request.open("GET", window.location);
request.send(null);
}
window.onload = function () {
var interval = 60000;
setInterval("reloadInfo()", interval);
}
答案 2 :(得分:0)
//find your Button like this instead of view
Button calculate =(Button)findViewById(R.id.calculate);
//use this below code for trigger button
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.calculate:
//write your code at here... it will work
break;
}
}
答案 3 :(得分:0)
删除此
private View calculate;
calculate = findViewById(R.id.calculate);
替换为
private Button calculate;
calculate =(Button) findViewById(R.id.calculate);