Android-- dynamic broadcast, radio static

1. The first built three static pages

2, and then click the button to jump to achieve

Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
            //Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。 
            //在存放资源代码的文件夹下下, 
            Intent i = new Intent(MainActivity.this , StaticActivity.class);
             //启动 
            startActivity(i);
           }
        });
android:onClick="onClick"

3, change the list of static content registration page

ListView features:
1. filled layout data;
2 processes the user selection operation such as clicking.

Displays a list requires three elements:
1. ListVeiw: View to display the list;
2. Adapter: used to map the data to the agency the ListView;
. 3. Source: specific string to be mapped, pictures, or basic components.

About adapter
adapter is connected to a data bridge and AdapterView effectively AdapterView separation data provided by it,
binding and so AdapterView data easier and more convenient modification. Adapting the data source to the ListView common adapter has:
the ArrayAdapter, SimpleAdapter and SimpleCursorAdapter.

Common data adapter (Adapter)
- When using ListView data needs to be adapted. To achieve this, Android system provides a series of adapters (Adapter) adapted to ListView data. - like display adapter, the complex data easily accepted by people to show the way.
BaseAdapter: namely, the basic adapter. It is actually an abstract class that has four abstract methods. In Android development, it is based on these methods abstract
data adaptation ListView

SimpleAdapter: SimpleAdapter inherited from BaseAdapter, implements four abstract methods BaseAdapter, namely getCount (), getItem (), getItemId (), getView () method.

ArrayAdapter: ArrayAdapter also inherited from BaseAdapter, and SimpleAdapter same. ArrayAdapter typically for adapting TextView control, such as setting menu (Setting) Android system.

package com.example.lenovo.broadcast;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class StaticActivity extends AppCompatActivity {

    private String[] data={"Banana","Cherry","coco","kiwi","Orange","Pear","Strawberry","Watermelon"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static);

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(StaticActivity.this,android.R.layout.simple_list_item_1,data);
        ListView listview=(ListView)findViewById(R.id.listView);
        listview.setAdapter(adapter);
    }
}
构建Intent,使用sendBroadcast方法发出广播定义一个广播接收器,
该广播接收器继承BroadcastReceiver,
并且覆盖onReceive()方法来响应事件注册该广播接收器,
我们可以在代码中注册(动态注册),
也可以AndroidManifest.xml配置文件中注册(静态注册)。

4, to achieve static registration function

MyReceiver.java file

kage com.example.lenovo.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast t = Toast.makeText(context,"静态广播:"+intent.getStringExtra("info"), Toast.LENGTH_SHORT);
        t.setGravity(Gravity.TOP,0,0);
        t.show();
    }
}

AndroidMainfest.xml modify configuration files

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                //自定义Action
                <action android:name="Pandora" />
            </intent-filter>
        </receiver>

5, dynamic registration function

6, dynamic registration click a button in the button text changes click

Some use Android studio found during the red solution R:
 1.New after an activity:

Create a new event when, Android studio will take a little time out to create this event, this period will have a progress bar at the bottom, before the progress bar to finish, if the code operation, it is likely to trigger this red mark R Happening;

2. Change the layout after the xml file:

File write software interface xml file, with logic closely related to the code, if there's xml code changes, which is likely to cause logic error code, where an error situation may be varied, and include the R marked red Happening.

3. there is time to reopen the closed project will be marked red ...

Sometimes this is due to lack of computer performance, running a project takes a long time, during this period, if you accidentally made some changes to the code, it may lead to R marked red.

Some solutions:

1. Restart software;

2. The re-marked red R hit a delete (Android studio I think it is not the software itself has a little problem, several times to knock back a deleted R is not being given a go ...);

3. Follow the prompts to find coding errors, an error code is sometimes true;

4. forced to run the project, sometimes one not two not being given a run;

Null pointer exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

· When using setContentView in Activity inside, while the definition of a number of other layout button, when in use, may cause a null pointer exception.
In the definition of control, it may require the assignment, but no assignment, carefully check each control is defined.

· When pasting someone else's code, sometimes paste the code layout and logic come together, but when referring to R file, not their layout in the id, but in other projects, that quote not in this project.

· Defined initialization string not assigned.

· C interface type specific object is not a class initialization, such as: List lt; being given List lt = new ArrayList (); is not being given.

· When to use the Bundle, and may not be assigned Intent to bind, and they receive data in another Activity inside, it will trigger a null pointer exception.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static);

R.layout. The following pages wrong, you should write a static xml instead main.xml.

Guess you like

Origin blog.csdn.net/Pandora_417/article/details/90813827