我有两个java文件。一个MainActivity文件和一个apclass文件。我想制作一个数学Android应用程序,它可以找到许多AP的术语,ap和其他的总和。
但问题是它给错误(红线)
我的主要活动
package com.pradeep.apgphp;
import android.support.design.widget.TabLayout;
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.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
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;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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();
return;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
apclass tab1 = new apclass();
return tab1;
case 1:
gpclass tab2 = new gpclass();
return tab2;
case 2:
hpclass tab3 = new hpclass();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "AP";
case 1:
return "GP";
case 2:
return "HP";
}
return null;
}
}
}
和我的AP CLASSA
package com.pradeep.apgphp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class apclass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ap, container, true);
return rootView;
}
}
现在,当我尝试添加按钮并希望发送一些值时,它会在其下面显示一条红线
按钮A =(按钮)findViewById(R.id.a);
如果我添加此类型,那么我将面临错误。
答案 0 :(得分:0)
如果您的按钮在班级a中,则需要编写以下代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ap, container, true);
Button A = (Button) rootview.findViewById(R.id.a);
return rootView;}
希望有所帮助:)
答案 1 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ap, container, false);
Button A = (Button) rootView.findViewById(R.id.a);
return rootView;
}
试试这个。
答案 2 :(得分:0)
你向你的apclass夸大了一个布局..现在你无法从它直接访问元素id。 当你使用
setContentView(R.layout.layoutname);
尝试
Button b1=(Button)findViewById(R.id.id);
当您为布局充气时,我们只能从视图中访问布局元素。所以请使用如下
Button b1=(Button)rootView.findViewById(R.id.id);
希望它能帮到你