Button mLoginButoon;
....
mLoginButoon = (Button)findViewById(R.string.login);
mLoginButoon为空。
这是代码: MainActivity:
package com.example.forum.controlers;
import com.example.forum.R;
import com.example.forum.R.id;
import com.example.forum.R.layout;
import com.example.forum.R.menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button mRegisterButoon;
private Button mLoginButoon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
setContentView(R.layout.activity_main);
o = this.findViewById(android.R.id.content);
mLoginButoon = (Button)findViewById(R.string.login);
mLoginButoon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
});
mRegisterButoon = (Button)findViewById(R.string.register);
mRegisterButoon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, LoginActivity.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);
}
}
这是activity_main布局:
<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: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.example.forum.controlers.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_to_my_forum" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal" >
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register" />
</LinearLayout>
答案 0 :(得分:1)
(Button)findViewById(R.string.login);
R.string? 改为使用R.id.login:
(Button)findViewById(R.id.login);
答案 1 :(得分:1)
您引用的是字符串资源而不是ID,例如您使用R.string.login
代替R.id.login
更改强>
mLoginButoon = (Button)findViewById(R.string.login);
以强>
mLoginButoon = (Button)findViewById(R.id.login);
和强>
mRegisterButoon = (Button)findViewById(R.string.register);
到
mRegisterButoon = (Button)findViewById(R.id.register);