gpt4 book ai didi

android - 在其他 Activity 中使用 On Button Click Listener 时出错

转载 作者:行者123 更新时间:2023-11-30 00:57:02 25 4
gpt4 key购买 nike

我是 Android 编程的新手。如果有人能帮助我,我会很高兴。

我在 SelectCity Activity 中使用提交按钮时出错。

这是我的主要 Activity 。

package com.example.dell.myop;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
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 com.example.dell.myop.R;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.dell.myop.fragments.OneFragment;

import com.example.dell.myop.fragments.ThreeFragment;
import com.example.dell.myop.fragments.TwoFragment;

public class MainActivity extends AppCompatActivity {


private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
public static SharedPreferences prefs;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
prefs = getSharedPreferences("PREFERENCES",MODE_PRIVATE);
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", true).commit();

Boolean isFirstRun = prefs.getBoolean("RUN",true);


if (isFirstRun) {
//show start activity
Intent intent = new Intent(MainActivity.this,SelectCity.class);
startActivity(intent);


Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();

}




}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Today");
adapter.addFragment(new TwoFragment(), "Tomorrow");
adapter.addFragment(new ThreeFragment(), "Day After");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

这是我的 SelectCity Activity

package com.example.dell.myop;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SelectCity extends AppCompatActivity {
EditText editText;
Button submitButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
editText = (EditText)findViewById(R.id.cityName);
submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("edit test value",editText.toString());

}
});

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_city);
}
}

最佳答案

您需要将 super.oncreate() 和 setcontentview(...在您的 selectCity 中,首先要调用 onCreate 方法;)

如下图

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_city);
editText = (EditText)findViewById(R.id.cityName);
submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("edit test value",editText.toString());

}
});
}

关于android - 在其他 Activity 中使用 On Button Click Listener 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com